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.
ggplot(data, aes( y, x, xmin, xmax )) + geom_point() + geom_errorbarh( height )
- 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.
Add Title and change axis label of 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.
plot() + labs( title, x, y )
title: determines the title of the plot.
x and y: determines the axis titles for the x-axis and y-axis respectively.
Example: Forest plot with a custom title for the plot and both axes.
Add a Vertical Line to the Plot
To add a vertical line to the plot as an overlay in the R Language by using the geom_vline() function. We can add a vertical line in the plot to show the position of zero for better visualization of the data. We can use the xintercept, linetype, color, and alpha argument of the geom_vline() function to customize the position, the shape of the line, color, and transparency of the vertical line respectively.
plot + geom_vline( xintercept, linetype, color, alpha )
- xintercept: determines the position of the line on the x-axis.
- linetype: determines the shape of the line.
- color: determines the color of the line.
- alpha: determines the transparency of the line.
Example: Forest plot with a vertical line at x=0.
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.
ggplot(data, aes(y, x, xmin, xmax)) + geom_errorbarh(height, color, lwd) + geom_point( color, pch, size)
- color: determines the color of point or errorbar
- lwd: determines the line width of error bar
- pch: determines the shape of point.
Here, is a completely customized forest plot.