Customization of Forest Plot

To customize the forest plot, we can change the color and shape of the bar and point to make it more informative as well as aesthetically pleasing. For changing color and size we can use basic aesthetic arguments such as color, lwd, pch, etc.

Syntax:

ggplot(data, aes(y, x, xmin, xmax)) +  geom_errorbarh(height, color, lwd) + geom_point( color, pch, size)

Parameter:

  • color: determines the color of point or errorbar
  • lwd: determines the line width of error bar
  • pch: determines the shape of point.

Example:

Here, is a completely customized forest plot.

R




# create sample data
sample_data <- data.frame(study=c('G1', 'G2', 'G3', 'G4', 'G5'),
                 index=1:5,
                 result=c(-.23, -.45, -.16, .6, .65),
                 error_lower=c(-.35, -.59, -.37, -.12, .24),
                 error_upper=c(-.17, -.25, -.03, .82, .91))
  
#load library ggplot2
library(ggplot2)
  
#create forest plot
ggplot(data=sample_data, aes(y=index, x=result, 
                             xmin=error_lower, 
                             xmax=error_upper)) + 
  geom_errorbarh(height=.1, color= "green", lwd=1.2) +
  geom_point( color= "red", pch= 9, size=3) +
  scale_y_continuous(labels=sample_data$study)+
  labs(title="Forest Plot")+
  geom_vline(xintercept=0, color='blue', linetype='dashed', alpha=.5)


Output:



How to Create a Forest Plot in R?

In this article, we will discuss how to create a Forest Plot in the R programming language.

A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in medical research for visualizing a meta-analysis of the results of randomized controlled trials. The x-axis of the plot contains the value of the interest in the studies and the y-axis displays the results from the different trials.

To create a Forest Plot in the R Language, we use a combination of scatter plots along with the error bar. The geom_point() function of the ggplot package helps us to create a scatter plot. To create an error bar plot as an overlay on top of the scatter plot, we use the geom_errorbarh() function. The geom_errorbarh() function is used to draw a horizontal error bar plot.

Syntax:

ggplot(data, aes( y, x, xmin, xmax )) + geom_point() +  geom_errorbarh( height )

Parameter:

  • data: determines the data frame to be used for plotting.
  • x and y: determines the axes variables
  • xmin and xmax: determines the x-axis limits
  • height: determines the width of the error bar

Example: Basic forest plot.

R




# create sample data
sample_data <- data.frame(study=c('G1', 'G2', 'G3', 'G4', 'G5'),
                 index=1:5,
                 result=c(-.23, -.45, -.16, .6, .65),
                 error_lower=c(-.35, -.59, -.37, -.12, .24),
                 error_upper=c(-.17, -.25, -.03, .82, .91))
  
#load library ggplot2
library(ggplot2)
  
#create forest plot
ggplot(data=sample_data, aes(y=index, x=result,
                             xmin=error_lower, 
                             xmax=error_upper)) +
  geom_point() + 
  geom_errorbarh(height=.1) +
  scale_y_continuous(labels=sample_data$study)


Output:

Similar Reads

Add Title and change axis label of Plot

...

Add a Vertical Line to the Plot

To add the title to the plot, we use the title argument of the labs() function of the R Language. We can also change axis labels of the x-axis and y-axis using the x and y argument of the labs() function respectively....

Customization of Forest Plot

...