# Forest plots using R and ggplot2

October 31, 2010
By

(This article was first published on Stat Bandit » R, and kindly contributed to R-bloggers)

Forest plots are most commonly used in reporting meta-analyses, but can be profitably used to summarise the results of a fitted model. They essentially display the estimates for model parameters and their corresponding confidence intervals.

Matt Shotwell just posted a message to the R-help mailing list with his lattice-based solution to the problem of creating forest plots in R. I just figured out how to create a forest plot for a consulting report using ggplot2. The availability of the `geom_pointrange` layer makes this process very easy!!

```credplot.gg <- function(d){
# 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
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)+ xlab('Variable')
return(p)
}```

```d <- data.frame(x = toupper(letters[1:10]),
y = rnorm(10, 0, 0.1))
d <- transform(d, ylo = y-1/10, yhi=y+1/10)

credplot.gg(d)```

we can get the following graph:

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...