Forest plots are commonly used in the medical research publications, especially in meta-analysis. And it can also be used to report the coefficients and confidence intervals (CIs) of the regression models.
There are lots of packages out there can be used to create draw a forest plot. The most popular one is forestpot. Packages specialised for the meta-analysis, like meta, metafor and rmeta. Some other packages, like ggforestplot, tried to use ggplot2 to draw a forest plot, they are not available on the CRAN yet.
The main differences of the
forestploter from the other
The layout of the forest plot is determined by the dataset provided.
The first step is to provide a
data.frame will be used
in the forest plot. Column names of the data will be drawn as the header
and 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 confidence interval. Space to draw the CI is
determined by the width of this column. Increase the number of space 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 blank column for the forest plot to display CI. # Adjust the column width with space. dt$` ` <- paste(rep(" ", 20), collapse = " ") # Create 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 the basic layout of the forest plot. The example below shows how to draw a simple forest plot by applying a theme. 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)