Here's the R code, slightly modified from Abhijit's version:
This file contains 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) |
And here's what you get:
Thanks for sharing the code, Abhiji!
Thank Stephen for re-posting. Hope this (very simple) code helps.
ReplyDeleteAbhijit
LETTERS[1:10] would also work to make upper case letters. This code snippet will be very helpful too, I have to make graphs like this often.
ReplyDeletei have finished my projects with half percent of your snippets and hscripts.com.i searched this for last two months. Thank you so much.
ReplyDeletedon't forget forestplot() and its relatives, in the rmeta package
ReplyDeleteThis is a nice example.
ReplyDeleteHowever, ff I alter your example data to have rs SNP names, then the resulting forest plot rearranges the SNPs into a different order.
d$x <- paste("rs",seq(1,10),sep="")
> d
x y ylo yhi
1 rs1 1.1729487 1.0729487 1.2729487
2 rs2 0.8283175 0.7283175 0.9283175
3 rs3 1.1198595 1.0198595 1.2198595
4 rs4 1.0222385 0.9222385 1.1222385
5 rs5 1.0053240 0.9053240 1.1053240
6 rs6 1.0113072 0.9113072 1.1113072
7 rs7 0.9460481 0.8460481 1.0460481
8 rs8 0.9916414 0.8916414 1.0916414
9 rs9 1.1097265 1.0097265 1.2097265
10 rs10 1.0737711 0.9737711 1.1737711
forestplot(d)
orders the SNPs from bottom to top as:
"rs1" "rs10" "rs2" "rs3" "rs4" "rs5" "rs6" "rs7" "rs8" "rs9"
How do I turn off the sorting by SNP name?
Thanks you.
Ah, not sure. I haven't used ggplot2 in some time now. I would recommend modifying the code to plot the x-axis based on an index that you assign to each of your rs numbers, and then relabel the axis using the actual rs number.
ReplyDelete