or <- c(1.081, 1.092, 1.095, 1.057, 1.086, 1.070, 1.116)
lower <- c(1.038, 1.052, 1.058, 1.019, 1.050, 1.036, 1.063)
upper <- c(1.126, 1.134, 1.134, 1.096, 1.123, 1.105, 1.171)
meta <- data.frame(
study = c("Chen (2016)", "Ibrahim (2022)",
"Bauer (2015)", "Evans (2018)", "Garcia (2020)", "Jensen (2023)",
"Fuentes (2019)"),
region = c("Asia", "Asia",
"Europe", "Europe", "Europe", "Europe",
"Latin America"),
or = or,
lower = lower,
upper = upper,
weight = c(2065, 1736, 816, 1041, 1479, 918, 567),
is_sum = FALSE,
or_text = sprintf("%.2f (%.2f–%.2f)", or, lower, upper)
)
meta
#> study region or lower upper weight is_sum or_text
#> 1 Chen (2016) Asia 1.081 1.038 1.126 2065 FALSE 1.08 (1.04–1.13)
#> 2 Ibrahim (2022) Asia 1.092 1.052 1.134 1736 FALSE 1.09 (1.05–1.13)
#> 3 Bauer (2015) Europe 1.095 1.058 1.134 816 FALSE 1.09 (1.06–1.13)
#> 4 Evans (2018) Europe 1.057 1.019 1.096 1041 FALSE 1.06 (1.02–1.10)
#> 5 Garcia (2020) Europe 1.086 1.050 1.123 1479 FALSE 1.09 (1.05–1.12)
#> 6 Jensen (2023) Europe 1.070 1.036 1.105 918 FALSE 1.07 (1.04–1.10)
#> 7 Fuentes (2019) Latin America 1.116 1.063 1.171 567 FALSE 1.12 (1.06–1.17)How forrest works
This vignette follows one data set through forrest(), calling the internal helpers directly to show what each step produces before anything is drawn. The helpers are not exported and are reached with :::; their interfaces may change between releases.
Design
forrest() is the only plotting function, and every layout is reached through its arguments, which name columns of the data as strings. The data stay tidy, one row per estimate. Visual structure such as section headers, indentation and blank rows is derived from grouping columns at draw time, so it never has to be written into the data.
All drawing uses base R graphics. The one dependency, tinyplot, sets up the plot region, so the axes and fonts follow tinyplot’s theming.
Source files
| File | Contents |
|---|---|
R/forrest.R |
forrest(): argument checks, section expansion, layout and drawing |
R/save.R |
save_forrest(): opens a PDF, PNG, SVG or TIFF device and draws into it |
R/utils.R |
build_sections(), compute_dodge_groups(), group_colors(), group_shapes(), check_col() and %||% |
R/draw.R |
draw_diamond() and draw_text_panel() |
R/theme.R |
.theme_defaults, .themes and resolve_theme() |
Example data
Seven studies from three regions, with odds ratios, weights and a formatted text column:
Without structural arguments every row is drawn as a study:
forrest(
meta,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
weight = "weight",
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Section expansion
When section is set, forrest() first calls build_sections(), which returns the expanded data frame that is then drawn, along with three logical vectors marking the rows it inserted.
expanded <- forrest:::build_sections(
df = meta,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
is_summary = "is_sum",
weight = "weight",
section = "region",
subsection = NULL,
section_indent = TRUE,
section_spacer = TRUE,
cols = "or_text",
section_cols = NULL
)
expanded$df[, c("study", "region", "or", "is_sum", "or_text")]
#> study region or is_sum or_text
#> 1 Asia <NA> NA FALSE
#> 2 Chen (2016) Asia 1.081 FALSE 1.08 (1.04–1.13)
#> 3 Ibrahim (2022) Asia 1.092 FALSE 1.09 (1.05–1.13)
#> 4 <NA> NA FALSE
#> 5 Europe <NA> NA FALSE
#> 6 Bauer (2015) Europe 1.095 FALSE 1.09 (1.06–1.13)
#> 7 Evans (2018) Europe 1.057 FALSE 1.06 (1.02–1.10)
#> 8 Garcia (2020) Europe 1.086 FALSE 1.09 (1.05–1.12)
#> 9 Jensen (2023) Europe 1.070 FALSE 1.07 (1.04–1.10)
#> 10 <NA> NA FALSE
#> 11 Latin America <NA> NA FALSE
#> 12 Fuentes (2019) Latin America 1.116 FALSE 1.12 (1.06–1.17)
#> 13 <NA> NA FALSEdata.frame(
study = expanded$df$study,
is_section_header = expanded$is_section_header,
is_subsection_hdr = expanded$is_subsection_header,
is_spacer = expanded$is_spacer
)
#> study is_section_header is_subsection_hdr is_spacer
#> 1 Asia TRUE FALSE FALSE
#> 2 Chen (2016) FALSE FALSE FALSE
#> 3 Ibrahim (2022) FALSE FALSE FALSE
#> 4 FALSE FALSE TRUE
#> 5 Europe TRUE FALSE FALSE
#> 6 Bauer (2015) FALSE FALSE FALSE
#> 7 Evans (2018) FALSE FALSE FALSE
#> 8 Garcia (2020) FALSE FALSE FALSE
#> 9 Jensen (2023) FALSE FALSE FALSE
#> 10 FALSE FALSE TRUE
#> 11 Latin America TRUE FALSE FALSE
#> 12 Fuentes (2019) FALSE FALSE FALSE
#> 13 FALSE FALSE TRUERows 1, 5 and 11 are the inserted headers for Asia, Europe and Latin America. Their label is the section value and their estimate is NA. A blank spacer row closes each section, the study labels are indented by two spaces, and the text column is empty on every inserted row.
Passing section = "region" to forrest() performs this expansion and draws the result:
forrest(
meta,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
section = "region",
weight = "weight",
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Before expanding, forrest() copies the group and shape columns, and a per-row col vector, into private columns. Expansion rewrites the label column, and group may name that same column; the copies keep colours and legends tied to the original values, and inserted rows get none.
Two levels
With subsection as well, build_sections() inserts a sub-header, indented by two spaces, wherever the subsection value changes within a section, and indents the data rows beneath it by four.
meta2 <- data.frame(
region = c("Europe", "Europe", "Europe", "Europe", "Asia", "Asia"),
design = c("Cohort", "Cohort", "Case-control", "Case-control",
"Cohort", "Case-control"),
study = c("Bauer (2015)", "Evans (2018)",
"Garcia (2020)", "Jensen (2023)",
"Chen (2016)", "Ibrahim (2022)"),
or = c(1.095, 1.057, 1.086, 1.070, 1.081, 1.092),
lower = c(1.058, 1.019, 1.050, 1.036, 1.038, 1.052),
upper = c(1.134, 1.096, 1.123, 1.105, 1.126, 1.134)
)
exp2 <- forrest:::build_sections(
df = meta2,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
is_summary = NULL,
weight = NULL,
section = "region",
subsection = "design",
section_indent = TRUE,
section_spacer = TRUE
)
data.frame(
study = exp2$df$study,
is_section_header = exp2$is_section_header,
is_subsection_header = exp2$is_subsection_header,
is_spacer = exp2$is_spacer
)
#> study is_section_header is_subsection_header is_spacer
#> 1 Europe TRUE FALSE FALSE
#> 2 Cohort FALSE TRUE FALSE
#> 3 Bauer (2015) FALSE FALSE FALSE
#> 4 Evans (2018) FALSE FALSE FALSE
#> 5 Case-control FALSE TRUE FALSE
#> 6 Garcia (2020) FALSE FALSE FALSE
#> 7 Jensen (2023) FALSE FALSE FALSE
#> 8 FALSE FALSE TRUE
#> 9 Asia TRUE FALSE FALSE
#> 10 Cohort FALSE TRUE FALSE
#> 11 Chen (2016) FALSE FALSE FALSE
#> 12 Case-control FALSE TRUE FALSE
#> 13 Ibrahim (2022) FALSE FALSE FALSE
#> 14 FALSE FALSE TRUEforrest(
meta2,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
section = "region",
subsection = "design",
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Values in header rows
section_cols fills chosen text columns on the header rows. forrest() translates its names, which are text-column headers, into the data columns those text columns display, and build_sections() writes into each one the first non-empty value of the source column among the section’s rows. Here the source column k_text holds the number of studies per section, and the displayed column k_row is empty on study rows.
meta$k_text <- c("k = 2", "", "k = 4", "", "", "", "k = 1")
meta$k_row <- ""
exp_sc <- forrest:::build_sections(
df = meta,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
is_summary = "is_sum",
weight = "weight",
section = "region",
section_cols = c(k_row = "k_text"),
cols = c("or_text", "k_row"),
section_spacer = FALSE,
section_indent = FALSE
)
exp_sc$df[, c("study", "or_text", "k_row")]
#> study or_text k_row
#> 1 Asia k = 2
#> 2 Chen (2016) 1.08 (1.04–1.13)
#> 3 Ibrahim (2022) 1.09 (1.05–1.13)
#> 4 Europe k = 4
#> 5 Bauer (2015) 1.09 (1.06–1.13)
#> 6 Evans (2018) 1.06 (1.02–1.10)
#> 7 Garcia (2020) 1.09 (1.05–1.12)
#> 8 Jensen (2023) 1.07 (1.04–1.10)
#> 9 Latin America k = 1
#> 10 Fuentes (2019) 1.12 (1.06–1.17)The header rows carry the count in k_row and nothing in or_text, which section_cols does not name; study rows keep their own values.
forrest(
meta,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
section = "region",
section_cols = c("k" = "k_text"),
weight = "weight",
log_scale = TRUE,
header = "Study",
cols = c("OR (95% CI)" = "or_text", "k" = "k_row"),
widths = c(3.5, 3.5, 2.2, 1.0),
xlab = "OR (95% CI)"
)
Row types
After expansion every row is one of four types, and the type decides what is drawn. Structural rows are the inserted headers and spacers; header labels are bold. Reference rows are rows of the data whose estimate is NA; they show a plain label and no interval. Summary rows, flagged by is_summary, are drawn as diamonds. All other rows get a point and an interval. The classification for the expanded example:
df <- expanded$df
est <- as.numeric(df$or)
is_sum <- as.logical(df$is_sum)
is_struct <- expanded$is_section_header |
expanded$is_subsection_header |
expanded$is_spacer
is_ref <- is.na(est) & !is_sum & !is_struct
is_bold <- (expanded$is_section_header | expanded$is_subsection_header) &
nchar(trimws(df$study)) > 0L
data.frame(
study = df$study,
is_sum = is_sum,
is_struct = is_struct,
is_ref = is_ref,
is_bold = is_bold,
ci_drawn = !is_sum & !is_struct & !is_ref
)
#> study is_sum is_struct is_ref is_bold ci_drawn
#> 1 Asia FALSE TRUE FALSE TRUE FALSE
#> 2 Chen (2016) FALSE FALSE FALSE FALSE TRUE
#> 3 Ibrahim (2022) FALSE FALSE FALSE FALSE TRUE
#> 4 FALSE TRUE FALSE FALSE FALSE
#> 5 Europe FALSE TRUE FALSE TRUE FALSE
#> 6 Bauer (2015) FALSE FALSE FALSE FALSE TRUE
#> 7 Evans (2018) FALSE FALSE FALSE FALSE TRUE
#> 8 Garcia (2020) FALSE FALSE FALSE FALSE TRUE
#> 9 Jensen (2023) FALSE FALSE FALSE FALSE TRUE
#> 10 FALSE TRUE FALSE FALSE FALSE
#> 11 Latin America FALSE TRUE FALSE TRUE FALSE
#> 12 Fuentes (2019) FALSE FALSE FALSE FALSE TRUE
#> 13 FALSE TRUE FALSE FALSE FALSEThis data set has no reference rows. A dose-response table has one, the reference category, and ref_label = TRUE appends “(Ref.)” to its label:
dose <- data.frame(
quartile = c("Q1", "Q2", "Q3", "Q4"),
or = c(NA, 1.21, 1.45, 1.82),
lower = c(NA, 1.08, 1.28, 1.60),
upper = c(NA, 1.36, 1.65, 2.07)
)
forrest(
dose,
estimate = "or",
lower = "lower",
upper = "upper",
label = "quartile",
ref_label = TRUE,
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Summary diamonds
draw_diamond() draws each summary row as a polygon whose left and right tips are the confidence limits and whose top and bottom points sit above and below the estimate, 0.38 * cex rows from the centre. Limits beyond xlim are cut at the axis.
with_pool <- rbind(
meta[, c("study", "region", "or", "lower", "upper", "is_sum")],
data.frame(
study = "Pooled", region = "Overall",
or = 1.082, lower = 1.058, upper = 1.107,
is_sum = TRUE
)
)
forrest(
with_pool,
estimate = "or",
lower = "lower",
upper = "upper",
label = "study",
section = "region",
is_summary = "is_sum",
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Dodging
compute_dodge_groups() numbers the bands that rows are drawn in. A new band starts at the first row, at every change of label, and at every structural row, so headers and spacers always have a band to themselves. Without dodge, each row is its own band.
dodge_ex <- data.frame(
label = rep(c("Asia", "Europe"), each = 2),
method = rep(c("Cohort", "Case-control"), 2),
or = c(1.08, 1.05, 1.09, 1.07),
lower = c(1.04, 1.01, 1.05, 1.03),
upper = c(1.13, 1.09, 1.14, 1.11)
)
band <- forrest:::compute_dodge_groups(dodge_ex$label,
rep(FALSE, nrow(dodge_ex)))
# Bands are one unit apart, top to bottom; rows in a band are spread
# symmetrically around its centre, `dodge` units apart.
n_band <- max(band)
centre <- (n_band + 1L) - band
k <- tabulate(band)[band]
pos <- ave(seq_along(band), band, FUN = seq_along)
y <- centre + (pos - (k + 1) / 2) * 0.25
data.frame(dodge_ex[, c("label", "method")], band, y)
#> label method band y
#> 1 Asia Cohort 1 1.875
#> 2 Asia Case-control 1 2.125
#> 3 Europe Cohort 2 0.875
#> 4 Europe Case-control 2 1.125The two Asia rows sit at 2 ± 0.125 and the two Europe rows at 1 ± 0.125:
forrest(
dodge_ex,
estimate = "or",
lower = "lower",
upper = "upper",
label = "label",
group = "method",
dodge = TRUE,
log_scale = TRUE,
xlab = "OR (95% CI)"
)
Colours
group_colors() assigns colours to the distinct non-missing group values in order of appearance. By default it uses the Okabe-Ito palette from its second colour on, because the first is black and would be confused with the near-black of ungrouped rows. After eight groups the palette repeats.
forrest:::group_colors(c("Asia", "Europe", NA, "Latin America"))
#> Asia Europe Latin America
#> "#E69F00" "#56B4E9" "#009E73"A col argument replaces the palette, matched by name or recycled in order:
forrest:::group_colors(c("Asia", "Europe"), c(Europe = "navy", Asia = "red"))
#> Asia Europe
#> "red" "navy"Themes
resolve_theme() starts from .theme_defaults and overwrites the keys that the chosen theme or list supplies. The six keys and their defaults:
forrest:::.theme_defaults
#> $grid_col
#> [1] "#e8e8e8"
#>
#> $grid_lty
#> [1] 1
#>
#> $grid_lwd
#> [1] 0.7
#>
#> $ref_col
#> [1] "gray45"
#>
#> $ref_lty
#> [1] 2
#>
#> $stripe_col
#> [1] "#f2f2f2"The built-in themes are lists of overrides:
forrest:::.themes
#> $default
#> list()
#>
#> $minimal
#> $minimal$grid_col
#> [1] "#f0f0f0"
#>
#> $minimal$grid_lwd
#> [1] 0.5
#>
#> $minimal$ref_col
#> [1] "#777777"
#>
#>
#> $classic
#> $classic$grid_col
#> [1] "lightgray"
#>
#> $classic$grid_lty
#> [1] 3
#>
#> $classic$grid_lwd
#> [1] 0.7
#>
#> $classic$ref_col
#> [1] "black"
#>
#> $classic$ref_lty
#> [1] 1
#>
#> $classic$stripe_col
#> [1] "#efefef"A list passed as theme overrides only the keys it names, and an unknown key is an error:
dat <- data.frame(
label = c("A", "B", "C"),
estimate = c(0.2, -0.1, 0.4),
lower = c(0.0, -0.3, 0.2),
upper = c(0.4, 0.1, 0.6)
)
forrest(
dat,
estimate = "estimate",
lower = "lower",
upper = "upper",
label = "label",
theme = list(ref_col = "#e63946", ref_lty = 1L,
grid_col = "#eeeeee", stripe_col = "#fafafa"),
stripe = TRUE,
xlab = "Coefficient (95% CI)"
)