Three exposure groups - wide 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 wide format, there is also a version in long 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 wide format, meaning each exposure group contributes with a set of columns/variables rather than rows/observations.

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

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

# point estimates and confidence interval limits for comparisons of groups 1 and 2 against group 0
print(results[,c(1,11:16)], digits = 5)
            level     rr1    lcl1    ucl1     rr2    lcl2   ucl2
           <char>   <num>   <num>   <num>   <num>   <num>  <num>
 1:       Overall 0.67524 0.62828 0.72571 0.99663 0.90424 1.0985
 2:           Sex      NA      NA      NA      NA      NA     NA
 3:        Female 0.63160 0.56358 0.70783 0.94637 0.81198 1.1030
 4:          Male 0.70788 0.64498 0.77690 1.03680 0.91418 1.1759
 5:           Age      NA      NA      NA      NA      NA     NA
 6:     <65 years 1.09769 0.77075 1.56331 1.02005 0.59393 1.7519
 7:     >65 years 0.66181 0.61565 0.71142 0.99665 0.90509 1.0975
 8: Heart failure      NA      NA      NA      NA      NA     NA
 9:            No 0.62879 0.57607 0.68634 1.04443 0.93230 1.1700
10:           Yes 0.82604 0.73261 0.93138 0.90706 0.75787 1.0856
11:  Hypertension      NA      NA      NA      NA      NA     NA
12:            No 0.62567 0.53434 0.73261 1.02799 0.83739 1.2620
13:           Yes 0.68834 0.63483 0.74637 0.98778 0.88456 1.1030

As in the other examples presented at this site, 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(
    # events / Number at risk
    en1 = case_when(is.na(events1) ~ "", .default = paste(events1, "/", n1)),
    en2 = case_when(is.na(events2) ~ "", .default = paste(events2, "/", n2)),
    en0 = case_when(is.na(events0) ~ "", .default = paste(events0, "/", n0)),
    
    # text variable with risk ratios
    riskratio1 = case_when(
      is.na(events1) ~ "",
      .default = paste0(round2(rr1), " (", round2(lcl1), " - ", round2(ucl1), ")")),
    riskratio2 = case_when(
      is.na(events2) ~ "",
      .default = paste0(round2(rr2), " (", round2(lcl2), " - ", round2(ucl2), ")")),
    
    # some indentation of some labels
    level = case_when(is.na(events1) | level == "Overall" ~ level, .default = paste(" ", level))
  ) |>
  # since there will be many columns, estimates of absolute risks are suppressed
  select(level, starts_with(c("en", "riskratio", "rr", "lcl", "ucl")))

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

# forcing some line breaks in column headers using \n
colnames(results) <- c(
  "Group",
  "Low dose\nEvents / N",
  "High dose\nEvents / N",
  "Comparison\nEvents / N",
  "Low dose vs comparison\nRisk ratio (95% CI)",
  "High dose vs comparison\nRisk ratio (95% CI)",
  "rr1",
  "rr2",
  "lcl1",
  "lcl2",
  "ucl1",
  "ucl2",
  "",
  ""
)

In this forest plot we will include the estimates from both comparisons in the same forest plot (see Figure 1).

forest(
  results[,c(1,13)],
  est = list(results$rr1, results$rr2),
  lower = list(results$lcl1, results$lcl2),
  upper = list(results$ucl1, results$ucl2),
  ci_column = 2,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5)
)
Figure 1: Forest plot with two sets of results in the same plot

As always we need to specify all the columns we want, do some specific modifications to some cells, etc.

tm <- forest_theme(
  base_size = 15,
  ci_pch = 19,
  ci_lwd = 3,
  
  # a color for each comparison to be used for point estimates and 95% CIs
  ci_col = c("#56B4E9","#001d6d"),
  
  # title in legend - set to missing
  legend_name = "",
  
  # labels to be used for the two comparisons, notice the space included to add some distance between labels
  legend_value = c("Low dose ", "High dose"),
  
  # place the legend below the plot
  legend_position = "bottom",
  refline_gp = gpar(col = "grey"),
  core = list(bg_params = list(fill = c("#e2f0fb", "white")))
)

p <- forest(
  results[,c(1,14,2,14,3,14,4,14,5,14,6,13)],
  est = list(results$rr1, results$rr2),
  lower = list(results$lcl1, results$lcl2),
  upper = list(results$ucl1, results$ucl2),
  ci_column = 12,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5),
  nudge_y = .25, # increase distance between points within strata
  theme = tm
)


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

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

# bold facing selected cells in first column
p <- edit_plot(
  p,
  col = 1,
  row = c(1, 2, 5, 8, 11),
  part = "body",
  which = "text",
  gp = gpar(fontface = "bold")
)
Figure 2: Forest plot with 3 exposure groups combined in one plot