The R-Code provided below is the brief introduction into how to create a forest plot with ggplot2 for regression estimates (Code: R-Code). If you have any questions about the R-Code please email me!
# Getting Started ####
library("ggplot2")
library("extrafont")
options(scipen=100, digits=6)
options(max.print = 500000000)
# Data for 1 forest plot (Estimates are Made up) ####
DV<-c("Y (N = 100)","Y (N = 100)","Y (N = 100)") # Heading for Facet Wrap
IV<-c("X1","X2","X3") # Independent variable names
ES<-c(.29,.11,.01) # b Estimate (could be standardized estimate, Odds Ratio, Incident Rate Ratio, etc.)
LCI<-c(.12,.01,-.07) # Lower 95% confidence interval
UCI<-c(.46,.21,.09) # Upper 95% confidence interval
A<-data.frame(DV,IV,ES,LCI,UCI)
A$IV<-factor(A$IV, levels=c("X3",
"X2",
"X1"))
# 1 Forest Plot ####
ggplot(data=A, aes(x=IV, y=ES, ymin=LCI, ymax=UCI)) +
geom_pointrange()+ # Makes range for ggplot values based on the data and AES specified in first line
geom_hline(yintercept=0, lty=2, size =1) + # add a dotted line at x=0 after flip
geom_errorbar(aes(ymin=LCI, ymax=UCI), width=0.5, cex=1)+ # Makes whiskers on the range (more aesthetically pleasing)
facet_wrap(~DV)+ # Makes DV header (Can handle multiple DVs)
coord_flip() + # flip coordinates (puts labels on y axis)
geom_point(shape = 15, size = 2) + # specifies the size and shape of the geompoint
ggtitle("")+ # Blank Title for the Graph
xlab("Independent Variables") + # Label on the Y axis (flipped specification do to coord_flip)
ylab("b (95% CI)") + # Label on the X axis (flipped specification do to coord_flip)
scale_y_continuous(limits = c(-.50,.50), breaks = c(-.50,-.25,0,.25,.50))+ # limits and tic marks on X axis (flipped specification do to coord_flip)
theme(line = element_line(colour = "black", size = 3), # My personal theme for GGplots
strip.background = element_rect(fill="gray90"),
legend.position ="none",
axis.line.x = element_line(colour = "black"),
axis.line.y = element_blank(),
panel.border= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 20, r = 0, b = 0, l = 0)),
axis.title.y = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 0, r = 20, b = 0, l = 0)),
plot.title = element_text(family="Times New Roman", colour = "Black", margin = margin(t = 0, r = 0, b = 20, l = 0)),
axis.text=element_text(family="Times New Roman",size=24, color = "Black"),
text=element_text(family="Times New Roman",size=24), plot.margin = margin(t = 2, r = 2, b = 2, l = 2, unit = "cm"))

# Getting Started ####
library("ggplot2")
library("extrafont")
options(scipen=100, digits=6)
options(max.print = 500000000)
# Data for 2 forest plot (Estimates are Made up) ####
DV<-c("Y1 (N = 100)","Y1 (N = 100)","Y1 (N = 100)","Y2 (N = 100)","Y2 (N = 100)","Y2 (N = 100)") # Heading for Facet Wrap
IV<-c("X1","X2","X3","X1","X2","X3") # Independent variable names
ES<-c(.29,.11,.01,-.51,-.23,.11) # b Estimate (could be standardized estimate, Odds Ratio, Incident Rate Ratio, etc.)
LCI<-c(.12,.01,-.07,-.73,-.48,.02) # Lower 95% confidence interval
UCI<-c(.46,.21,.09,-.29,.02,.20) # Upper 95% confidence interval
A<-data.frame(DV,IV,ES,LCI,UCI)
A$IV<-factor(A$IV, levels=c("X3",
"X2",
"X1"))
# 2 Forest Plots ####
ggplot(data=A, aes(x=IV, y=ES, ymin=LCI, ymax=UCI)) +
geom_pointrange()+ # Makes range for ggplot values based on the data and AES specified in first line
geom_hline(yintercept=0, lty=2, size =1) + # add a dotted line at x=0 after flip
geom_errorbar(aes(ymin=LCI, ymax=UCI), width=0.5, cex=1)+ # Makes whiskers on the range (more aesthetically pleasing)
facet_wrap(~DV)+ # Makes DV header (Can handle multiple DVs)
coord_flip() + # flip coordinates (puts labels on y axis)
geom_point(shape = 15, size = 2) + # specifies the size and shape of the geompoint
ggtitle("")+ # Blank Title for the Graph
xlab("Independent Variables") + # Label on the Y axis (flipped specification do to coord_flip)
ylab("b (95% CI)") + # Label on the X axis (flipped specification do to coord_flip)
scale_y_continuous(limits = c(-.75,.75), breaks = c(-.75,-.38,0,.38,.75))+ # limits and tic marks on X axis (flipped specification do to coord_flip)
theme(line = element_line(colour = "black", size = 1), # My personal theme for GGplots
strip.background = element_rect(fill="gray90"),
legend.position ="none",
axis.line.x = element_line(colour = "black"),
axis.line.y = element_blank(),
panel.border= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.spacing = unit(2, "lines"), # added to theme to add space inbetween facet_wrap plots
axis.ticks = element_blank(),
axis.title.x = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 20, r = 0, b = 0, l =0)),
axis.title.y = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 0, r = 20, b = 0, l = 0)),
plot.title = element_text(family="Times New Roman", colour = "Black", margin = margin(t = 0, r = 0, b = 20, l = 0)),
axis.text=element_text(family="Times New Roman",size=24, color = "Black"),
text=element_text(family="Times New Roman",size=24), plot.margin = margin(t = 2, r = 2, b = 2, l = 2, unit = "cm"))

License: Creative Commons Attribution 4.0 International (CC By 4.0)