## Wednesday, March 9, 2011

### 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:

And here's what you get:

Thanks for sharing the code, Abhiji!

#### 6 comments:

1. Thank Stephen for re-posting. Hope this (very simple) code helps.

Abhijit

2. 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.

3. i have finished my projects with half percent of your snippets and hscripts.com.i searched this for last two months. Thank you so much.

4. don't forget forestplot() and its relatives, in the rmeta package

5. This is a nice example.

However, 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.

6. 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.

Note: Only a member of this blog may post a comment.

Getting Genetics Done by Stephen Turner is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.