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:

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

...