How to Create a Forest Plot in R


A forest plot (sometimes called a “blobbogram”) is used in a meta-analysis to visualize the results of several studies in one plot.

The x-axis displays the value of interest in the studies (often an odds ratio, effect size, or mean difference) and the y-axis displays the results from each individual study.

This type of plot offers a convenient way to visualize the results of several studies all at once.

The following example shows how to create a forest plot in R.

Example: Forest Plot in R

To create a forest plot in R, we need to first create a data frame to hold the effect size (or whatever value of interest) and the upper and lower confidence intervals for each study:

#create data
df <- data.frame(study=c('S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7'),
                 index=1:7,
                 effect=c(-.4, -.25, -.1, .1, .15, .2, .3),
                 lower=c(-.43, -.29, -.17, -.02, .04, .17, .27),
                 upper=c(-.37, -.21, -.03, .22, .24, .23, .33))

#view data
head(df)

  study index effect lower upper
1    S1     1  -0.40 -0.43 -0.37
2    S2     2  -0.25 -0.29 -0.21
3    S3     3  -0.10 -0.17 -0.03
4    S4     4   0.10 -0.02  0.22
5    S5     5   0.15  0.04  0.24
6    S6     6   0.20  0.17  0.23
7    S7     7   0.30  0.27  0.33

Next, we can use functions from the ggplot2 data visualization package to create the following forest plot:

#load ggplot2
library(ggplot2)

#create forest plot
ggplot(data=df, aes(y=index, x=effect, xmin=lower, xmax=upper)) +
  geom_point() + 
  geom_errorbarh(height=.1) +
  scale_y_continuous(name = "", breaks=1:nrow(df), labels=df$study)

The x-axis displays the effect size for each study and the y-axis displays the name of each study.

The points in the plot displays the effect size for each study and the error bars show the confidence interval bounds.

Note that we can also add a title, modify the axis labels, and add a vertical line at an effect size of zero to make the chart more aesthetically pleasing:

#load ggplot2
library(ggplot2)

#create forest plot
ggplot(data=df, aes(y=index, x=effect, xmin=lower, xmax=upper)) +
  geom_point() + 
  geom_errorbarh(height=.1) +
  scale_y_continuous(breaks=1:nrow(df), labels=df$study) +
  labs(title='Effect Size by Study', x='Effect Size', y = 'Study') +
  geom_vline(xintercept=0, color='black', linetype='dashed', alpha=.5) +
  theme_minimal()

Forest plot in R

Feel free to modify the theme of the plot to make it look however you’d like. For example, we could also use theme_classic() for an even more classic appearance:

#load ggplot2
library(ggplot2)

#create forest plot
ggplot(data=df, aes(y=index, x=effect, xmin=lower, xmax=upper)) +
  geom_point() + 
  geom_errorbarh(height=.1) +
  scale_y_continuous(breaks=1:nrow(df), labels=df$study) +
  labs(title='Effect Size by Study', x='Effect Size', y = 'Study') +
  geom_vline(xintercept=0, color='black', linetype='dashed', alpha=.5) +
  theme_classic()

Additional Resources

How to Create a Forest Plot in Excel
A Complete Guide to the Best ggplot2 Themes

3 Replies to “How to Create a Forest Plot in R”

  1. Does R limit the number of strings to 7?
    I have created a wonderful plot with seven pieces of data, on trying to add in an eighth, I get an error code.
    However, I am yet to find a forest plot on R with more than 7 separate pieces of information.

    Many thanks!

  2. Does R limit the number of strings to 7?
    I have created a wonderful plot with seven pieces of data, on trying to add in an eighth, I get an error code.
    However, I am yet to find a forest plot on R with more than 7 separate pieces of information.

    Many thanks!

    1. Hi Dan…R does not impose a strict limit of seven strings or data points for forest plots. However, the issue you’re encountering might be related to **graphical parameters**, **layout limitations**, or **data formatting** rather than a hard-coded limitation in R. Let’s explore why this might be happening and how to resolve it.

      ### Common Causes for Forest Plot Errors with More than Seven Data Points
      1. **Graphical Layout Issues**:
      – If you’re using `grid` or `ggplot2`-based functions to create the forest plot, the layout parameters might need adjustment to accommodate additional rows of data.

      2. **Character Vector Limits in Input**:
      – Some forest plot packages (like `meta` or `forestplot`) expect inputs as character vectors or data frames. If one of these inputs is improperly formatted or misaligned with others, it could cause errors when adding more data points.

      3. **Error Due to Margins or Space**:
      – Adding more rows might cause the plot to run out of space if the margins (`mar`) or text size (`cex`) are not adjusted accordingly.

      4. **Package-Specific Constraints**:
      – Some older or simpler implementations of forest plots may default to displaying a limited number of rows unless explicitly configured.

      ### Steps to Resolve the Issue

      #### 1. **Check the Input Data**
      Ensure that your data is properly formatted. For instance:
      “`r
      data <- data.frame( Label = c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), Mean = c(1.2, 1.5, 1.1, 1.3, 1.4, 1.6, 1.8, 1.9), Lower = c(1.0, 1.2, 0.9, 1.1, 1.2, 1.4, 1.5, 1.6), Upper = c(1.4, 1.8, 1.3, 1.5, 1.6, 1.9, 2.1, 2.3) ) ``` #### 2. **Adjust Graphical Parameters** If the error is due to space constraints, try adjusting the `mar` or `cex` parameters: ```r par(mar = c(5, 10, 4, 2)) # Increase margins for better fit ``` You can also reduce the text size with `cex`: ```r forestplot( labeltext = data$Label, mean = data$Mean, lower = data$Lower, upper = data$Upper, cex = 0.8 # Reduce text size ) ``` #### 3. **Use a More Flexible Library** If you're using a base R package like `meta` or `forestplot`, ensure it's updated or switch to a more modern alternative like `ggplot2` for greater flexibility: ```r library(ggplot2) library(ggforestplot) ggforestplot::forest_plot( data, estimate = Mean, lower = Lower, upper = Upper, label = Label ) ``` #### 4. **Inspect the Error Message** The specific error code or message will provide clues. For example: - If it's a graphical limit issue: Adjust layout or size parameters. - If it's a data formatting issue: Ensure all inputs have the correct length and type. #### 5. **Add a Scrollable Output (Optional)** For large datasets, consider breaking your plot into chunks or creating an interactive/scrollable version using tools like `plotly` or `shiny`. --- ### Example with More than Seven Data Points Here's a simple example with eight rows: ```r library(forestplot) data <- data.frame( Label = paste("Group", 1:8), Mean = c(1.2, 1.5, 1.1, 1.3, 1.4, 1.6, 1.8, 1.9), Lower = c(1.0, 1.2, 0.9, 1.1, 1.2, 1.4, 1.5, 1.6), Upper = c(1.4, 1.8, 1.3, 1.5, 1.6, 1.9, 2.1, 2.3) ) forestplot( labeltext = data$Label, mean = data$Mean, lower = data$Lower, upper = data$Upper, cex = 0.8, xlab = "Effect Size" ) ``` --- If you share the exact error message or code you're working with, I can provide more targeted assistance! 😊

Leave a Reply

Your email address will not be published. Required fields are marked *