Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created February 11, 2011 02:59
Show Gist options
  • Save stephenturner/821850 to your computer and use it in GitHub Desktop.
Save stephenturner/821850 to your computer and use it in GitHub Desktop.
forestplot.r
# 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)
@RMHogervorst
Copy link

This doesn't work anymore with the new ggplot2.
I suggest the following change:

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(yintercept = 1) +
                ylab(xlab) +
                xlab(ylab) #switch because of the coord_flip() above
        return(p)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment