How to Create a Forest Plot in R?
Last Updated :
04 Jan, 2022
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:

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.
Syntax:
plot() + labs( title, x, y )
Parameter:
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.
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)+
labs(title='Title Of Plot', x='X-axis Title', y = 'Y-axis Title')
Output:

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.
Syntax:
plot + geom_vline( xintercept, linetype, color, alpha )
Parameter:
- 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.
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)+
geom_vline(xintercept=0, color='green', linetype='dashed', alpha=.8)
Output:

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:
Similar Reads
How to Create a Forest Plot in Excel?
Forest plots are an excellent way to convey a multitude of information in a single picture. Forest plots have become a recognized and well-understood technique of displaying several estimates concurrently, whether used to demonstrate various outcomes in a single research or the cumulative knowledge
4 min read
How to Create a Frequency Polygon in R?
In this article, we will discuss how to create a Frequency Polygon in the R Programming Language. Frequency polygons are the plots of the values in a data frame to visualize the shape of the distribution of the values. It helps us in comparing different data frames and visualizing the cumulative fre
3 min read
How to Create a Log-Log Plot in R?
In this article, we will discuss how to create a Log-Log plot in the R Programming Language. A log-log plot is a plot that uses logarithmic scales on both the axes i.e., the x-axis and the y-axis.We can create a Log-Log plot in the R language by following methods. Log-Log Plot in Base R: To create a
2 min read
How to create Kernel Density Plot in R?
In this article, we will discuss how to create kernel density plots in R programming language. For this, the user simply needs to call the density() function which is an in-build function in R language. Then the user has to pass the given data as the parameter to this function in order to create a d
5 min read
How to Plot Categorical Data in R?
In this article, we will be looking at different plots for the categorical data in the R programming language. Categorical Data is a variable that can take on one of a limited, and usually fixed, a number of possible values, assigning each individual or other unit of observation to a particular grou
3 min read
How to create dataframe in R
Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
How to create a reusable plot_ly function in R
In data visualization, reusability and consistency are crucial for maintaining clarity and efficiency. Plotly is the powerful library in the R for creating interactive plots. By encapsulating the plotting logic into the reusable functions. We can streamline the plotting process and it can ensure uni
5 min read
How to plot excel data in R?
Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read
How to Create Added Variable Plots in R?
In this article, we will discuss how to create an added variable plot in the R Programming Language. The Added variable plot is an individual plot that displays the relationship between a response variable and one predictor variable in a multiple linear regression model while controlling for the pre
3 min read
How To Annotate a Plot with Circle in R
In this article, we will discuss how to Annotate a Plot with Circle in R Programming Language. We can do by using ggplot2 package  Aesthetic mappings can be created to the plot object to determine the relationship between the x and y axis respectively. Additional components can be added to the creat
2 min read