Created
February 11, 2011 02:59
-
-
Save stephenturner/821850 to your computer and use it in GitHub Desktop.
forestplot.r
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work anymore with the new ggplot2.
I suggest the following change: