Three exposure groups - long version

Setting

Here is an example of a forest plot containing combined results of risk ratios for a comparison of three exposure groups (high dose, low dose, control, say) from a hypothetical study. These data are in long format, there is also a version in wide format. Some familiarity with the forestploter package is assumed. The reader might want to look at the simple forest plot before continuing.

Data

This version of the data is in long format, meaning each exposure group contributes with a row/observation for each subgroup that has been analyzed.

library(forestploter)
library(grid)
library(data.table)
library(dplyr)

results <- fread("./data/TwoComparisonFPdataLong.csv")

# overview of selected variables - first 10 observations
print(results[1:10,c(1:4,6:8)], digits = 5)
         level     x     n events      rr     lcl     ucl
        <char> <int> <int>  <int>   <num>   <num>   <num>
 1:    Overall    NA    NA     NA      NA      NA      NA
 2:                0 20400   1634      NA      NA      NA
 3:                1 22150   1198 0.67524 0.62828 0.72571
 4:                2  6063    484 0.99663 0.90424 1.09847
 5: Sex Female    NA    NA     NA      NA      NA      NA
 6:                0  9243    681      NA      NA      NA
 7:                1 10143    472 0.63160 0.56358 0.70783
 8:                2  2811    196 0.94637 0.81198 1.10301
 9:   Sex Male    NA    NA     NA      NA      NA      NA
10:                0 11157    953      NA      NA      NA

The data needs a bit of editing before it is ready to be plugged into forest.

round2 <- function(x) format(round(x,digits = 2), nsmall = 2, scientific = FALSE)

results <- results |>
  mutate(
    x = case_when(x == 0 ~ "Control",
                  x == 1 ~ "Low dose",
                  x == 2 ~ "High dose",
                  .default = ""),
    eventsn = case_when(is.na(events) ~ "",
                        .default = paste(events, "/", n)),
    riskratio = case_when(
      is.na(events) ~ "",
      is.na(rr) ~ "(ref)",
      .default = paste0(round2(rr), " (", round2(lcl), " - ", round2(ucl), ")"))
  ) |>
  select(level, x, eventsn, risk, riskratio, rr, lcl, ucl)

# empty spaces necessary for the forest function
results$blank <- "                          "
results$smallblank <- " "

colnames(results) <- c(
  "",
  "Exposure group",
  "Events / N",
  "Risk (95% CI)",
  "",
  "rr",
  "lcl",
  "ucl",
  "",
  ""
)

The data are ready to be plotted. In the theme we specify 4 colors for stripe-alternation; dark for subgroup header, white-light-white for specific exposure groups.

tm <- forest_theme(
  base_size = 15,
  ci_pch = 19, 
  ci_lwd = 3,
  ci_col = "#58508d",
  refline_gp = gpar(col = "grey"),
  # 4 alternating colors of stripes
  core = list(bg_params = list(fill = c("#ccc4e2", "white", "#f3eeff", "white")))
)

p <- forest(
  results[,c(1,2,10,3,10,4,10,5,9)],
  est = results$rr,
  lower = results$lcl,
  upper = results$ucl,
  ci_column = 9,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5),
  theme = tm
)

# add header for risk ratio / forest plot
p <- add_text(
  p,
  col = 8:9,
  row = 1,
  part = "header",
  text = "Risk ratio (95% CI)",
  gp = gpar(fontface = "bold", fontsize = 15)
)

# center aligning columns
p <- edit_plot(
  p,
  col = 4:8,
  part = "header",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

p <- edit_plot(
  p,
  col = 4:8,
  part = "body",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

# bold facing first column
p <- edit_plot(
  p,
  col = 1,
  part = "body",
  which = "text",
  gp = gpar(fontface = "bold")
)
Figure 1: Forest plot with 3 exposure groups