Introduction to forestploter

Alimu Dayimu

2024-04-13

Forest plots are commonly used in medical research publications, especially in meta-analysis. It can also be used to report the coefficients and confidence intervals (CIs) of the regression models.

There are lots of packages out there that can be used to draw a forest plot. The most popular one is forestplot. Packages specialized for the meta-analysis, like meta, metafor, and rmeta. Some other packages, like ggforestplot use ggplot2 to draw a forest plot, it is not available on the CRAN yet.

The main differences between the forestploter from the other packages are:

Basic forest plot

The layout of the forest plot is determined by the dataset provided. Please check out the other vignette if you want to change the text or background, add or insert text, add borders to cells, and edit the color of the CI in some cells.

Simple forest plot

The first step is to prepare a data.frame to be used as a basic layout of the forest plot. Column names of the data will be drawn as the header, and the contents inside the data will be displayed in the forest plot. One or multiple blank columns without any content (blanks) should be provided to draw a confidence interval. The width of the box to draw the CI is determined by the width of this column. Increase the number of spaces in the column to give more space to draw CI.

First, we need to get the data ready to plot.

library(grid)
library(forestploter)

# Read provided sample example data
dt <- read.csv(system.file("extdata", "example_data.csv", package = "forestploter"))

# Keep needed columns
dt <- dt[,1:6]

# Indent the subgroup if there is a number in the placebo column
dt$Subgroup <- ifelse(is.na(dt$Placebo), 
                      dt$Subgroup,
                      paste0("   ", dt$Subgroup))

# NA to blank or NA will be transformed to carachter.
dt$Treatment <- ifelse(is.na(dt$Treatment), "", dt$Treatment)
dt$Placebo <- ifelse(is.na(dt$Placebo), "", dt$Placebo)
dt$se <- (log(dt$hi) - log(dt$est))/1.96

# Add a blank column for the forest plot to display CI.
# Adjust the column width with space, and increase the number of spaces below 
# to have a larger area to draw the CI. 
dt$` ` <- paste(rep(" ", 20), collapse = " ")

# Create a confidence interval column to display
dt$`HR (95% CI)` <- ifelse(is.na(dt$se), "",
                             sprintf("%.2f (%.2f to %.2f)",
                                     dt$est, dt$low, dt$hi))
head(dt)
#>          Subgroup Treatment Placebo      est        low       hi        se
#> 1    All Patients       781     780 1.869694 0.13245636 3.606932 0.3352463
#> 2             Sex                         NA         NA       NA        NA
#> 3            Male       535     548 1.449472 0.06834426 2.830600 0.3414741
#> 4          Female       246     232 2.275120 0.50768005 4.042560 0.2932884
#> 5             Age                         NA         NA       NA        NA
#> 6          <65 yr       297     333 1.509242 0.67029394 2.348190 0.2255292
#>                                                   HR (95% CI)
#> 1                                         1.87 (0.13 to 3.61)
#> 2                                                            
#> 3                                         1.45 (0.07 to 2.83)
#> 4                                         2.28 (0.51 to 4.04)
#> 5                                                            
#> 6                                         1.51 (0.67 to 2.35)

The data we have above will be used as the basic layout of the forest plot. The example below shows how to draw a simple forest plot. A footnote was added as a demonstration.

p <- forest(dt[,c(1:3, 8:9)],
            est = dt$est,
            lower = dt$low, 
            upper = dt$hi,
            sizes = dt$se,
            ci_column = 4,
            ref_line = 1,
            arrow_lab = c("Placebo Better", "Treatment Better"),
            xlim = c(0, 4),
            ticks_at = c(0.5, 1, 2, 3),
            footnote = "This is the demo data. Please feel free to change\nanything you want.")

# Print plot
plot(p)