Pages

Forest plots using R and ggplot2

Abhijit over at Stat Bandit posted some nice code for making forest plots using ggplot2 in R. You see these lots of times in meta-analyses, or as seen in the BioVU demonstration paper. The idea is simple - on the x-axis you have the odds ratio (or whatever stat you want to show), and each line is a different study, gene, SNP, phenotype, etc. You draw a dot for the odds ratio point estimate, and lines extending from that point showing the confidence limits for that odds ratio.

Here's the R code, slightly modified from Abhijit's version:

# d is a data frame with 4 columns
# d$x gives variable names
# d$y gives center point
# d$ylo gives lower limits
# d$yhi gives upper limits
forestplot <- function(d, xlab="Odds Ratio", ylab="Study"){
require(ggplot2)
p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi)) +
geom_pointrange() +
coord_flip() +
geom_hline(aes(x=0), lty=2) +
ylab(xlab) +
xlab(ylab) #switch because of the coord_flip() above
return(p)
}
# Create some dummy data.
d <- data.frame(x = toupper(letters[1:10]),
y = rnorm(10, .05, 0.1))
d <- transform(d, ylo = y-1/10, yhi=y+1/10)
forestplot(d)
view raw forestplot.r hosted with ❤ by GitHub


And here's what you get:


Thanks for sharing the code, Abhiji!