A simple forest plot

Setting

Here is a walk through of how the forestploter package can be used to make a simple forest plot of risk ratios from a study comparing 2 exposure groups. The figure will also include numbers of events and numbers at risk along with risk estimates from the two groups.

Data

We use data from a hypothetical study when two groups have been compared with respect to the risk of some outcome:

library(data.table) # just to load data
library(forestploter) # to make the forest plots
library(grid) # necessary for the forestploter package to work
library(dplyr) # for data manipulation

results <- fread("./data/SimpleFPdata.csv")
results[,-c(4,7)]
            level    n1 events1    n0 events0        rr       lcl       ucl
           <char> <int>   <int> <int>   <int>     <num>     <num>     <num>
 1:       Overall 17179     936 31434    2514 0.6812592 0.6334847 0.7326368
 2:           Sex    NA      NA    NA      NA        NA        NA        NA
 3:        Female  7947     363 14250    1027 0.6337939 0.5641010 0.7120971
 4:          Male  9232     573 17184    1487 0.7172526 0.6535601 0.7871523
 5:           Age    NA      NA    NA      NA        NA        NA        NA
 6:     <65 years  6868      49 12667      91 0.9931119 0.7026062 1.4037325
 7:     >65 years 10311     887 18767    2423 0.6662915 0.6193427 0.7167992
 8: Heart failure    NA      NA    NA      NA        NA        NA        NA
 9:            No 15522     639 28202    1811 0.6410841 0.5870626 0.7000767
10:           Yes  1657     297  3232     703 0.8240434 0.7293777 0.9309957
11:  Hypertension    NA      NA    NA      NA        NA        NA        NA
12:            No  5117     183  9385     583 0.5757079 0.4893394 0.6773206
13:           Yes 12062     753 22049    1931 0.7128240 0.6572110 0.7731428

As seen, there are results from an overall analysis, and analyses stratified on sex, age, heart failure and hypertension. Hidden from the output are two columns with risk estimates.

Essentially, we need to print this data in a table with a forest plot to one side.

Empty spaces

Using the forestploter package you need to have a column with no content. This is where the actual forest plot will be inserted, therefore the empty column generally needs to be rather wide. As space between columns with data can be small it is often also a good idea to have a more narrow empty column, which can be used several times if necessary as a buffer between columns:

results$blank <- "                          "
results$smallblank <- " "

First version

Now, let’s try to make a forest plot including a column with labels indicating the analysis, risk estimates for each exposure group, and the forest plot.

forest(
  results[,c(1,4,7,11)],
  est = results$rr,
  lower = results$lcl,
  upper = results$ucl,
  ci_column = 4,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5)
)
Figure 1: Rough outline of a forest plot

Clearly there is still some work to be done, but before we do that let’s consider what forest does. Think of the output as a table with 4 columns: first the labels, then the risk estimates in exposure groups 1 and 2, then a column with no data. It is into this last column we have inserted the forest plot. Going through the parameters:

  • data: here we specify the data we want to use. Notice that we pick a subset of our data set results to be used by writing [,c(1,4,7,11)] after results. Specifically, we use all rows from the data (there is nothing between [ and the first comma), and then we pick the 1st, 4th, 7th and 11th columns in the order they appear in results. The 11th column is the blank column we created above.
  • est, lower, and upper: specify the variables used to plot the point estimate and confidence interval.
  • ci_column: here we specify into which column the plot should be inserted. Here, ci_column = 4 refers to the fact that four columns [c(1,4,7,11)] are selected from the data and that plot should be inserted into the last of those listed, i.e., column number 11 from the data.
  • ref_line: inserts a vertical reference line at the specified value.
  • xlim and ticks_at: specify the range of the axis used in the plot, and where there should be tick marks.

Number of events and number at risk

We want to include three additional columns into the plot, the exposure-group specific numbers of events and at risk (combined to one column per group), and the risk ratio. Let’s also indent labels for subgroups.

# we want exactly 2 decimal places for all risk ratios and CI-limits, we therefore make a new function.
round2 <- function(x) format(round(x,digits = 2), nsmall = 2, scientific = FALSE)

results <- results |>
  mutate(
    # events and N at risk in the two groups
    en1 = case_when(is.na(events1) ~ "", .default = paste(events1, "/", n1)),
    en0 = case_when(is.na(events0) ~ "", .default = paste(events0, "/", n0)),
    
    # risk ratio with 95% CI
    risk_ratio = case_when(
      is.na(events0) ~ "",
      .default = paste0(round2(rr), " (", round2(lcl), " - ", round2(ucl), ")")
    ),
    
    # indentation of selected levels
    level = case_when(is.na(events1) | level == "Overall" ~ level, .default = paste(" ", level))
  ) |>
  select(level, en1, risk1, en0, risk0, risk_ratio, rr, lcl, ucl, blank, smallblank)

Let’s make a second version of the forest plot including the extra columns. Notice that because we now use more columns, we need to change where to insert the forest plot (ci_column). This parameter will change several times in the following code snippets.

p <- forest(
  data = results[,c(1:6,10)],
  est = results$rr,
  lower = results$lcl,
  upper = results$ucl,
  ci_column = 7,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5)
)
Figure 2: Plot including all data

Extra padding between columns

With the current settings it seems redundant, however, for the sake of the example a bit of space is inserted between the 1st and 2nd, between then 3rd and 4th, and between the 5th and 6th columns. We use the smallblank column for this.

p <- forest(
  data = results[,c(1,11,2,3,11,4,5,11,6,10)], # column 11 (smallblank) inserted several times for padding
  est = results$rr,
  lower = results$lcl,
  upper = results$ucl,
  ci_column = 10,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5)
)
Figure 3: Plot with added padding

Column headers

This looks exceptionally stupid. Figure 3 holds all the data we want to include but the smallblank-columns take up way too much space. However, editing the column headers will help with this. We will edit these by naming the columns (variables) of the data set results:

colnames(results) <- c(
  "Group",
  "Events / N at risk",
  "Risk (95% CI)",
  "Events / N at risk",
  "Risk (95% CI)",
  "",
  "rr",
  "lcl",
  "ucl",
  "",
  ""
)

Notice how we shamelessly use the same, sometime empty, name for several columns. This is intentional.

If we once again run the forest function on these data we get Figure 4.

Figure 4: Plot with custom headers

Further headers

Exposure group headers

In Figure 4 we can’t tell which columns refer to which exposure group. We can make that clear by manipulating the plot p using the functions add_text and add_border from the forestploter package. Specifically, we want to add additional headers spanning several columns. We also add a line between data and headers for each exposure group - this is probably not strictly necessary in this situation, but it can be helpful to guide the eye.

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

# we add a header for the intervention- and exposure groups
p <- add_text(
  p,
  text = "Intervention group",
  col = 3:4, # the text should be across these columns
  row = 0, # ... and in this row
  part = "header",
  gp = gpar(fontface = "bold", fontsize = 15) # style of the text - see documentation of gpar for further details
)

p <- add_text(
  p,
  text = "Control group",
  col = 6:7,
  row = 0,
  part = "header",
  gp = gpar(fontface = "bold", fontsize = 15)
)

# we add a line between the header and the data for both exposure groups
p <- add_border(
  p,
  part = "body",
  gp = gpar(lwd = .5), # line width
  row = 0,
  col = c(3, 4, 6, 7)
)
Figure 5: Plot with exposure group headers

Combined header for risk ratio and plot

The last header we want to add is one spanning the columns with risk ratios (reported numerically and graphically)

p <- add_text(
  p,
  text = "Risk ratio (95% CI)",
  col = 9:10,
  row = 1,
  part = "header",
  gp = gpar(fontface = "bold")
)
Figure 6: Plot with header for risk ratios

Finishing touches

There are still several things that would improve the plot. In the following we

  • center-align columns containing numbers,
  • make labels without indentation bold face,
  • specify a theme using theme_forest to modify general aspects of the plot (next code chunk).
# center aligning the numbers (body) in the plot
p <- edit_plot(
  p,
  col = 3:7,
  part = "body",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

# center aligning the headers in the plot
p <- edit_plot(
  p,
  col = 3:7,
  part = "header",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

# bold facing selected labels
p <- edit_plot(
  p,
  col = 1,
  row = c(1, 2, 5, 8, 11),
  part = "body",
  which = "text",
  gp = gpar(fontface = "bold")
)

The theme is something that needs to be added when running forest meaning this should be done at the top of the script. Here is the entire script necessary to make Figure 7, once the data has been set up:

# specifying the theme
tm <- forest_theme(
  base_size = 15, # size of text
  ci_pch = 19, # shape of point estimate (19 = circle)
  ci_lwd = 3.5, # line width of CI
  ci_col = "#58508d", # color of the point estimate and CI
  refline_gp = gpar(col = "grey"), # color of reference line
  core = list(bg_params = list(fill = c("#f6f1ff", "white"))) # alternating colors of stripes
)

# constructing the basic forest plot
p <- forest(
  data = results[,c(1,11,2,3,11,4,5,11,6,10)],
  est = results$rr,
  lower = results$lcl,
  upper = results$ucl,
  ci_column = 10,
  ref_line = 1,
  xlim = c(.5, 1.5),
  ticks_at = c(.5, .75, 1, 1.25, 1.5),
  theme = tm # use the custom theme
)

# add headers
p <- add_text(
  p,
  text = "Intervention group",
  col = 3:4,
  row = 0,
  part = "header",
  gp = gpar(fontface = "bold", fontsize = 15)
)

p <- add_text(
  p,
  text = "Control group",
  col = 6:7,
  row = 0,
  part = "header",
  gp = gpar(fontface = "bold", fontsize = 15)
)

p <- add_text(
  p,
  text = "Risk ratio (95% CI)",
  col = 9:10,
  row = 1,
  part = "header",
  gp = gpar(fontface = "bold", fontsize = 15)
)

# add borders under selected headers
p <- add_border(
  p,
  part = "body",
  gp = gpar(lwd = .5), #line width
  row = 0,
  col = c(3, 4, 6, 7)
)

# center aligning the numbers (body) in the plot
p <- edit_plot(
  p,
  col = 3:7,
  part = "body",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

# center aligning the headers in the plot
p <- edit_plot(
  p,
  col = 3:7,
  part = "header",
  which = "text",
  hjust = unit(.5, "npc"),
  x = unit(.5, "npc")
)

# bold facing selected labels
p <- edit_plot(
  p,
  col = 1,
  row = c(1, 2, 5, 8, 11),
  part = "body",
  which = "text",
  gp = gpar(fontface = "bold")
)
Figure 7: Finished forest plot with custom theme