Skip to main content

Forest plots for meta-analyses

In a meta-analysis forest plot each row is one study, the size of its marker reflects the study’s weight, and a diamond at the bottom shows the pooled estimate. This vignette builds that plot and its common variants for an invented meta-analysis of long-term exposure to fine particulate matter (PM2.5) and incident hypertension. The study names and numbers are made up.

forrest() only draws; it does not pool. Compute the pooled estimates with a meta-analysis package such as metafor or meta, or by hand as below, and pass the results in as rows.

Data

Each study contributes a log odds ratio per 10 μg/m³ of PM2.5 and its standard error, chosen to scatter around a log odds ratio of 0.08. The pooled row is a fixed-effect (inverse-variance) estimate.

meta_dat <- data.frame(
  study    = c(
    "Anderson et al. (2014)",
    "Bauer et al. (2015)",
    "Chen et al. (2016)",
    "Di et al. (2017)",
    "Evans et al. (2018)",
    "Fuentes et al. (2019)",
    "Garcia et al. (2020)",
    "Hart et al. (2021)",
    "Ibrahim et al. (2022)",
    "Jensen et al. (2023)"
  ),
  region   = c(
    "North America", "Europe", "Asia", "North America", "Europe",
    "Latin America", "Europe", "North America", "Asia", "Europe"
  ),
  n        = c(4520, 2810, 8340, 12600, 3950, 1870, 5430, 9100, 6780, 3210),
  events   = c( 612,  390, 1120,  1950,  540,  255,  770, 1380, 980,  450),
  log_or   = c(0.062, 0.091, 0.078, 0.095, 0.055, 0.110, 0.083,
               0.072, 0.088, 0.068),
  se       = c(0.028, 0.035, 0.022, 0.019, 0.031, 0.042, 0.026,
               0.021, 0.024, 0.033),
  is_sum   = FALSE
)

# Odds ratios and 95% CIs on the natural scale
meta_dat$or    <- exp(meta_dat$log_or)
meta_dat$lower <- exp(meta_dat$log_or - 1.96 * meta_dat$se)
meta_dat$upper <- exp(meta_dat$log_or + 1.96 * meta_dat$se)

# Inverse-variance weights
meta_dat$weight <- 1 / meta_dat$se^2

pooled_log_or <- with(meta_dat, sum(weight * log_or) / sum(weight))
pooled_se     <- sqrt(1 / sum(meta_dat$weight))

pooled_row <- data.frame(
  study    = "Pooled (fixed-effect)",
  region   = NA_character_,
  n        = sum(meta_dat$n),
  events   = sum(meta_dat$events),
  log_or   = pooled_log_or,
  se       = pooled_se,
  or       = exp(pooled_log_or),
  lower    = exp(pooled_log_or - 1.96 * pooled_se),
  upper    = exp(pooled_log_or + 1.96 * pooled_se),
  weight   = NA_real_,
  is_sum   = TRUE
)
meta_all <- rbind(meta_dat, pooled_row)

# Text for the right-hand columns
meta_all$or_text     <- sprintf("%.2f (%.2f–%.2f)",
                                meta_all$or, meta_all$lower, meta_all$upper)
meta_all$n_text      <- formatC(meta_all$n,      format = "d", big.mark = ",")
meta_all$events_text <- formatC(meta_all$events, format = "d", big.mark = ",")

Basic plot

Odds ratios belong on a log axis, where an odds ratio of 2 and one of 0.5 sit at the same distance from 1. With log_scale = TRUE the reference line moves to 1 by default. weight scales the marker so that its area is proportional to the study’s weight, and is_summary turns the pooled row into a diamond.

forrest(
  meta_all,
  estimate   = "or",
  lower      = "lower",
  upper      = "upper",
  label      = "study",
  is_summary = "is_sum",
  weight     = "weight",
  log_scale  = TRUE,
  xlab       = "OR for hypertension per 10 μg/m³ PM2.5 (95% CI)"
)

Text columns

Published meta-analysis plots usually print the sample size, event count and estimate beside each study. cols adds these as text columns and header labels the study column.

forrest(
  meta_all,
  estimate   = "or",
  lower      = "lower",
  upper      = "upper",
  label      = "study",
  is_summary = "is_sum",
  weight     = "weight",
  log_scale  = TRUE,
  header     = "Study",
  cols       = c(
    "N"            = "n_text",
    "Events"       = "events_text",
    "OR (95% CI)"  = "or_text"
  ),
  widths     = c(3.5, 3.5, 1.2, 1.2, 2.2),
  xlab       = "OR for hypertension per 10 μg/m³ PM2.5 (95% CI)"
)

Colour by region

Colouring the study rows by region shows at a glance whether estimates cluster geographically. The pooled row is left out here because it belongs to no region.

study_dat <- meta_all[!meta_all$is_sum, ]

forrest(
  study_dat,
  estimate   = "or",
  lower      = "lower",
  upper      = "upper",
  label      = "study",
  group      = "region",
  log_scale  = TRUE,
  xlab       = "OR for hypertension per 10 μg/m³ PM2.5 (95% CI)"
)

Subgroup analysis by region

For a formal subgroup analysis, each region gets its own header, its studies and its own pooled diamond. section = "region" inserts the headers, so the data only need the study rows and one pooled row per region, sorted by region.

# Fixed-effect pooled row for one region
pool_region <- function(dat, region_label) {
  w   <- 1 / dat$se^2
  lp  <- sum(w * dat$log_or) / sum(w)
  se  <- sqrt(1 / sum(w))
  data.frame(
    study    = sprintf("%s (pooled)", region_label),
    region   = region_label,
    n        = sum(dat$n),
    events   = sum(dat$events),
    log_or   = lp,
    se       = se,
    or       = exp(lp),
    lower    = exp(lp - 1.96 * se),
    upper    = exp(lp + 1.96 * se),
    weight   = sum(w),
    is_sum   = TRUE
  )
}

regions    <- c("Asia", "Europe", "Latin America", "North America")
structured <- do.call(rbind, lapply(regions, function(r) {
  sub <- meta_dat[meta_dat$region == r, ]
  rbind(sub, pool_region(sub, r))
}))

structured$or_text <- sprintf(
  "%.2f (%.2f–%.2f)",
  structured$or, structured$lower, structured$upper
)

forrest(
  structured,
  estimate   = "or",
  lower      = "lower",
  upper      = "upper",
  label      = "study",
  section    = "region",
  is_summary = "is_sum",
  weight     = "weight",
  log_scale  = TRUE,
  header     = "Study",
  cols       = c("OR (95% CI)" = "or_text"),
  widths     = c(4, 3.5, 2.5),
  stripe     = TRUE,
  xlab       = "OR for hypertension per 10 μg/m³ PM2.5 (95% CI)"
)

Latin America has a single study, so its pooled estimate equals that study’s estimate.

Several exposures in the same studies

When the same studies report several exposures, one band per study with one interval per exposure lets the reader compare exposures within a study. The data have one row per study and exposure, sorted by study; group colours the exposures and dodge = TRUE offsets them within each band.

set.seed(99)
studies6 <- c("Anderson (2018)", "Bauer (2019)", "Chen (2020)",
              "Di (2021)", "Evans (2022)", "Fuentes (2023)")

make_exp <- function(true_lor, n = 6) {
  se  <- runif(n, 0.025, 0.045)
  lor <- rnorm(n, true_lor, 0.02)
  data.frame(
    or    = exp(lor),
    lower = exp(lor - 1.96 * se),
    upper = exp(lor + 1.96 * se)
  )
}

multi_exp <- cbind(
  data.frame(
    study    = rep(studies6, 3),
    exposure = rep(c("PM2.5", "NO2", "Noise"), each = 6)
  ),
  rbind(make_exp(0.08), make_exp(0.05), make_exp(0.03))
)
multi_exp <- multi_exp[order(match(multi_exp$study, studies6)), ]

forrest(
  multi_exp,
  estimate   = "or",
  lower      = "lower",
  upper      = "upper",
  label      = "study",
  group      = "exposure",
  dodge      = TRUE,
  log_scale  = TRUE,
  header     = "Study",
  xlab       = "OR for hypertension (95% CI)"
)

Saving plots

save_forrest(
  file   = "meta_analysis_forest.pdf",
  plot   = function() {
    forrest(
      meta_all,
      estimate   = "or",
      lower      = "lower",
      upper      = "upper",
      label      = "study",
      is_summary = "is_sum",
      weight     = "weight",
      log_scale  = TRUE,
      xlab       = "OR for hypertension per 10 μg/m³ PM2.5 (95% CI)"
    )
  },
  width  = 10,
  height = 6
)