| tabforest | R Documentation |
tabforest() is the common forest-plot engine for R4VN. It can (1) fit
regression models directly from an outcome and focal predictors, (2) reuse a
fitted R4VN or standard R model, (3) place several outcomes side by side using
the same predictor structure, and (4) create subgroup-effect forests with a
p-value for interaction. Numeric estimates are always stored without clipping;
xmin and xmax affect only the drawing.
tabforest(
outcome = NULL,
predictors = NULL,
data = NULL,
time = NULL,
event = NULL,
failure = NULL,
outcomes = NULL,
subgroup = NULL,
predictor = NULL,
type = c("auto", "regression", "multioutcome", "subgroup"),
crude = TRUE,
adjusted = FALSE,
multi = FALSE,
or = FALSE,
rr = FALSE,
pr = FALSE,
irr = FALSE,
estimate = c("auto", "beta", "or", "rr", "pr", "irr", "hr"),
ci = 0.95,
sample = c("auto", "common", "model"),
per = NULL,
per_labels = NULL,
select = NULL,
xmin = NULL,
xmax = NULL,
ticks = NULL,
log = NULL,
arrows = TRUE,
row_layout = c("auto", "modelrows", "compact"),
layout = c("dodge", "stack"),
row_spacing = 1,
model_row_gap = 0.55,
group_gap = 0.25,
order = NULL,
reference = TRUE,
pvalue = TRUE,
global_p = FALSE,
show_n = FALSE,
show_events = FALSE,
show_model_label = TRUE,
show_interaction_p = TRUE,
p_layout = c("inline", "column"),
effect_digit = 2,
p_digit = 3,
labels = NULL,
level_labels = NULL,
lang = c("en", "vi"),
text = NULL,
model_labels = NULL,
label_title = NULL,
effect_title = NULL,
axis_title = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
note = TRUE,
template = c("journal", "clean", "minimal"),
grid = c("major", "none", "both"),
font_family = "",
base_size = 11,
label_cex = 1,
header_cex = 1,
axis_cex = 1,
model_cex = 0.86,
colors = NULL,
fills = NULL,
pch = NULL,
lty = NULL,
point_cex = 1.15,
point_lwd = 1,
ci_lwd = 1.2,
ref_lwd = 1,
ref_lty = 2,
ref_col = "gray45",
arrow_length = 0.08,
zebra = FALSE,
zebra_fill = c("white", "gray94"),
zebra_by = c("variable", "header"),
label_width = 0.28,
forest_width = 0.32,
column_gap = 0.012,
panel_gap = 0.012,
panel_forest_ratio = 0.58,
label_indent = 0.018,
label_wrap = 38,
file = NULL,
width = 12,
height = NULL,
dpi = 300,
show = TRUE,
console = FALSE
)
outcome |
Outcome variable for ordinary regression/subgroup analysis, or a
supported fitted object ( |
predictors |
Focal predictors that should appear in the forest. Prefer
|
data |
Data frame. If omitted, the active R4VN data frame is used. |
time |
Follow-up time variable for Cox regression. Supplying |
event |
Modeled event level for binary OR/RR/PR outcomes. |
failure |
Event value for Cox regression. With numeric 0/1 status, 1 is selected automatically when present. |
outcomes |
Optional named character vector or named list for a
multi-outcome forest. Character example: |
subgroup |
Optional categorical variables for subgroup analysis. When
supplied, use |
predictor |
Main exposure for subgroup mode. It may be continuous or a
two-level categorical variable. Use |
type |
Analysis mode: |
crude |
For regression/multi-outcome mode, fit one crude model per focal
predictor. Set |
adjusted |
Regression mode: |
multi |
Regression mode: |
or, rr, pr, irr |
Logical shortcuts for OR, RR, PR, or IRR. Only one may be TRUE. Binary outcomes default to OR; numeric outcomes default to beta. |
estimate |
Explicit effect type: |
ci |
Confidence level, default 0.95. |
sample |
Missing-data strategy. |
per |
Optional multiplier for continuous effects. Example |
per_labels |
Optional display labels for |
select |
Model components when |
xmin, xmax |
Forest plotting limits. These never alter stored estimates.
For ratio effects, if only |
ticks |
Optional axis ticks. In multi-outcome mode a named list can give different ticks to different outcome panels. |
log |
|
arrows |
Draw arrowheads when CIs extend beyond plotting limits. When the point estimate itself is outside the range, no false boundary point is drawn. |
row_layout |
|
layout |
In compact mode, |
row_spacing |
Baseline distance between ordinary rows. |
model_row_gap |
Distance between model rows belonging to the same
variable/level when |
group_gap |
Extra vertical separation between variable blocks. |
order |
Optional order of focal predictor variable names. |
reference |
Show categorical reference rows. Default TRUE. |
pvalue |
Show coefficient-level p-values. |
global_p |
Show categorical-variable omnibus Wald p-values. Default FALSE because forest plots are usually cleaner without these values. |
show_n, show_events |
Add model N and number of events after the estimate. |
show_model_label |
In |
show_interaction_p |
In subgroup mode, show the p-value for interaction on the subgroup-variable header row. |
p_layout |
Regression display style: |
effect_digit, p_digit |
Decimal places for effects and p-values. |
labels |
Named character vector overriding variable labels. |
level_labels |
Named list overriding displayed categorical levels. This changes display only, not model coding/reference levels. |
lang |
Built-in language: |
text |
Named list overriding individual words. Useful keys include
|
model_labels |
Named character vector overriding model labels, e.g.
|
label_title, effect_title, axis_title |
Optional column/axis titles. |
title, subtitle, caption |
Optional plot title, subtitle, caption. |
note |
TRUE for an automatic clipping note, FALSE for none, or custom text. |
template |
Visual preset: |
grid |
|
font_family |
Base graphics font family. |
base_size, label_cex, header_cex, axis_cex, model_cex |
Text-size controls. |
colors |
Model line/marker colors. A named vector is recommended, e.g.
|
fills |
Optional marker fill colors, useful with pch 21:25. |
pch |
Model marker symbols. Named vectors may assign different symbols to crude and adjusted estimates. |
lty |
Model CI line types. |
point_cex |
Model marker sizes. May be scalar or named vector by model. |
point_lwd |
Marker border widths. May be scalar or named vector. |
ci_lwd |
CI line widths. May be scalar or named vector by model. |
ref_lwd, ref_lty, ref_col |
Null-line appearance. |
arrow_length |
Arrowhead size in inches. |
zebra |
Draw alternating background blocks by predictor/subgroup. |
zebra_fill |
Two or more background colors, e.g.
|
zebra_by |
|
label_width, forest_width, column_gap |
Horizontal layout controls for a single regression/subgroup forest. |
panel_gap |
Gap between panels in multi-outcome mode. |
panel_forest_ratio |
Fraction of each multi-outcome panel devoted to the CI forest; the remaining panel width is used for numeric estimates. |
label_indent |
Indentation of categorical levels. |
label_wrap |
Approximate wrapping width for long labels; Inf disables. |
file |
Optional PDF/PNG/SVG/JPG/TIFF output file. |
width, height, dpi |
Graphics dimensions. If height is NULL it grows with
the actual number of drawn rows, so |
show |
Draw immediately. Default TRUE. |
console |
Print the long standardized estimate table. |
With predictors=vars(A,B,C):
crude=TRUE: Y~A, Y~B, Y~C.
adjusted=vars(X): Y~A+X, Y~B+X, Y~C+X.
adjusted=TRUE: each focal variable is adjusted for the other focal vars.
multi=TRUE: one joint model Y~A+B+C.
multi=vars(A,B,C,X): one joint model Y~A+B+C+X, but only A/B/C are shown.
Therefore adjusted and multi answer different scientific questions and may
be requested together in the same forest.
When two or more model groups are displayed, row_layout="auto" uses separate
model rows and ONE shared effect column. For example, Crude and Multivariable
ORs are both printed under the same OR (95% CI) header instead of being put
in separate Crude-OR and Multivariable-OR columns.
numeric continuous outcome -> linear regression -> beta, null=0;
binary outcome -> logistic regression -> OR, null=1;
pr=TRUE -> robust modified Poisson -> PR, null=1;
rr=TRUE -> robust modified Poisson -> RR, null=1;
count outcome + irr=TRUE -> Poisson -> IRR, null=1;
time= -> Cox proportional hazards -> HR, null=1.
Suppose OR=7.41 and 95% CI=1.56 to 35.20 while xmax=10. The printed number
remains 7.41 (1.56-35.20). Only the graphical CI is truncated at 10 and an
arrow is drawn. If OR itself exceeds 10, no marker is placed falsely at 10.
row_layout="auto" is the default. If more than one model group is present,
it automatically switches to the "modelrows" layout. Crude, Adjusted and/or
Multivariable estimates are placed on separate physical rows, while the right
side contains only ONE shared effect column such as OR (95% CI), HR (95% CI),
or Beta (95% CI). The result, CI line, marker and p-value therefore stay on
exactly the same row. This is the recommended publication layout when crude and
adjusted estimates are presented together. Increase row_spacing,
model_row_gap, group_gap, or leave height=NULL for a taller figure.
Set row_layout="compact" only when you intentionally want several model
estimates on the same labelled row; compact mode retains separate numeric model
columns because the rows are not expanded.
outcomes= creates side-by-side panels sharing predictor labels. Every panel
may have its own effect type, axis range and follow-up variable. This permits
two binary outcomes (OR panels), several continuous outcomes (beta panels), or
even mixed OR/HR panels in one figure. For very many panels, increase width.
subgroup= estimates the effect of one main predictor separately within
each subgroup level. A full model containing predictor*subgroup is fitted for
the Wald interaction p-value. For RR/PR, the interaction Wald test uses the
same robust covariance approach as the effect model. Cox subgroup forests use
HR and a Cox interaction model. A subgroup variable must be categorical; make
clinically meaningful groups before calling tabforest().
colors, fills, pch, lty, point_cex, point_lwd, and ci_lwd accept
named model vectors. zebra=TRUE shades complete variable blocks, closely
matching journal forest-table layouts. lang="vi", text=, labels=, and
level_labels= allow all visible wording to be translated without changing
the model.
An object of class r4vn_tabforest; multi-outcome and subgroup modes
add subclasses r4vn_tabforest_multi and r4vn_tabforest_subgroup.
$data is publication-ready, $table is the numeric long table, $models
stores fitted models, and plot() can redraw without refitting.
vars, tab, tabmulti, tabsurv, tabexport
Other R4VN tables:
tab(),
tabexport(),
tablong(),
tabmeta(),
tabmulti(),
tabscale(),
tabscore(),
tabsurvey(),
vars()
data(tabforest_demo)
usedf(tabforest_demo, quiet = TRUE)
# Crude odds ratios.
f1 <- tabforest(
hypertension,
predictors = vars(c.age, sex, c.bmi, smoking),
event = "Yes",
show = FALSE
)
f1$data
# Crude plus one final multivariable model.
f2 <- tabforest(
hypertension,
predictors = vars(c.age, sex, c.bmi, smoking),
event = "Yes",
crude = TRUE, multi = TRUE,
show = FALSE
)
# Re-drawing is intentionally interactive so CRAN examples do not depend
# on the graphics device or installed fonts.
if (interactive()) {
plot(f2, row_layout = "modelrows", zebra = TRUE)
}
# Each focal predictor adjusted for the same confounders.
f3 <- tabforest(
hypertension,
predictors = vars(c.age, c.bmi, smoking),
event = "Yes",
adjusted = vars(sex, education),
show = FALSE
)
# Vietnamese display text can be prepared without drawing during checks.
f_vi <- tabforest(
hypertension,
predictors = vars(c.age, sex, c.bmi, smoking),
event = "Yes",
multi = TRUE,
lang = "vi",
labels = c(
age = "Tu\u1ed5i",
sex = "Gi\u1edbi t\u00ednh",
bmi = "Ch\u1ec9 s\u1ed1 kh\u1ed1i c\u01a1 th\u1ec3",
smoking = "H\u00fat thu\u1ed1c"
),
level_labels = list(
sex = c(Female = "N\u1eef", Male = "Nam"),
smoking = c(No = "Kh\u00f4ng", Yes = "C\u00f3")
),
text = list(reference = "Tham chi\u1ebfu"),
title = "Bi\u1ec3u \u0111\u1ed3 forest",
show = FALSE
)
if (interactive()) plot(f_vi)
# Modified-Poisson prevalence ratio.
f_pr <- tabforest(
depression,
predictors = vars(c.age, sex, smoking, alcohol),
event = "Yes", pr = TRUE, multi = TRUE,
show = FALSE
)
# Continuous outcome.
f_beta <- tabforest(
sbp,
predictors = vars(c.age, sex, c.bmi, smoking),
multi = TRUE,
show = FALSE
)
# Cox model, only when the suggested package is available.
if (requireNamespace("survival", quietly = TRUE)) {
f_hr <- tabforest(
death, time = followup,
predictors = vars(c.age, sex, treatment, c.bmi),
failure = 1, crude = TRUE, multi = TRUE,
show = FALSE
)
}
# Multi-outcome forest without drawing.
f_multi <- tabforest(
outcomes = c(
Hypertension = "hypertension",
Depression = "depression"
),
predictors = vars(c.age, sex, c.bmi, smoking),
event = "Yes", crude = FALSE, multi = TRUE,
show = FALSE
)
# Subgroup forest without drawing.
f_sub <- tabforest(
hypertension,
predictor = vars(treatment),
subgroup = vars(age_group, sex, obesity, diabetes, smoking),
event = "Yes", type = "subgroup",
adjusted = vars(c.age, c.bmi),
show = FALSE
)
# File output uses a temporary path and is cleaned up.
f_png <- tempfile(fileext = ".png")
tabforest(
hypertension,
predictors = vars(c.age, sex, c.bmi, smoking),
event = "Yes", multi = TRUE,
file = f_png, width = 8, height = 5, dpi = 120,
show = FALSE
)
unlink(f_png)
usedf(clear = TRUE, quiet = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.