-
Notifications
You must be signed in to change notification settings - Fork 12
/
forest_plot.Rmd
100 lines (80 loc) · 5.03 KB
/
forest_plot.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Forest plots in R (ggplot) with side table
========================================================
A friend asked me to help with a forest plot recently. After chatting about what she wanted the end result to look like, this is what I came up with.
```{r results='hide', tidy=F, echo=FALSE}
library(ggplot2)
library(gridExtra)
dat <- data.frame(group = factor(c("A","B","C","D","E","F","G"), levels=c("F","E","D","C","B","A","G")),
cen = c(3.1,2.0,1.6,3.2,3.6,7.6,NA),
low = c(2,0.9,0.8,1.5,2,4.2,NA),
high = c(6,4,2,6,5,14.5,NA))
theme_set(theme_bw())
theme_update(
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")
)
p <- ggplot(dat,aes(cen,group)) +
geom_point(size=5, shape=18) +
geom_errorbarh(aes(xmax = high, xmin = low), height = 0.15) +
geom_vline(xintercept = 1, linetype = "longdash") +
scale_x_continuous(breaks = seq(0,14,1), labels = seq(0,14,1)) +
labs(x="Adjusted Odds Ratio", y="")
lab <- data.frame(V0 = factor(c("A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G"),, levels=c("G","F","E","D","C","B","A")),
V05 = rep(c(1,2,3,4),each=7),
V1 = c("Occuption","Active","","Inactive","","Inactive","","Recreation","Inactive","","Active","","Inactive","","Gender","Men","Women","Men","Women","Men","Women","OR",3.1,2.0,1.6,3.2,3.6,7.6)
)
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) +
theme(panel.grid.major = element_blank(),
legend.position = "none",
panel.border = element_blank(),
axis.text.x = element_text(colour="white"),#element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_line(colour="white"),#element_blank(),
plot.margin = unit(c(0,0,0,0), "lines")) +
labs(x="",y="") +
coord_cartesian(xlim=c(1,4.5))
```
```{r forest_plot_1, fig.width=8, fig.height=4, tidy=F}
grid.arrange(data_table, p, ncol=2)
```
Some other options for forest plots included this:
From [Abhijit](http://statbandit.wordpress.com/2010/11/01/forest-plots-using-r-and-ggplot2/) blog. But I figured I'd start a fresh, and I'd already borrowed [some code](http://mcfromnz.wordpress.com/2012/05/05/kaplan-meier-survival-plot-with-at-risk-table-by-sub-groups/) of Abhijits in the past for a survival curve.
The package [rmeta](http://cran.r-project.org/web/packages/rmeta/index.html) offers two different versions. One with forestplot like so:
```{r forest_plot_2, fig.width=6, fig.height=6, tidy=F, echo=FALSE}
library(rmeta)
data(cochrane)
steroid <- meta.MH(n.trt, n.ctrl, ev.trt, ev.ctrl,
names=name, data=cochrane)
tabletext<-cbind(c("","Study",steroid$names,NA,"Summary"),
c("Deaths","(steroid)",cochrane$ev.trt,NA,NA),
c("Deaths","(placebo)", cochrane$ev.ctrl, NA,NA),
c("","OR",format(exp(steroid$logOR),digits=2),NA,format(exp(steroid$logMH),digits=2))
)
m<- c(NA,NA,steroid$logOR,NA,steroid$logMH)
l<- m-c(NA,NA,steroid$selogOR,NA,steroid$selogMH)*2
u<- m+c(NA,NA,steroid$selogOR,NA,steroid$selogMH)*2
forestplot(tabletext,m,l,u,zero=0,is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE),
clip=c(log(0.1),log(2.5)), xlog=TRUE,
col=meta.colors(box="royalblue",line="darkblue", summary="royalblue"))
```
And one with metaplot like so:
```{r forest_plot_3, fig.width=6, fig.height=6, tidy=F, echo=FALSE}
data(catheter)
a <- meta.MH(n.trt, n.ctrl, col.trt, col.ctrl, data=catheter,
names=Name, subset=c(13,6,5,3,7,12,4,11,1,8,10,2))
metaplot(a$logOR, a$selogOR, nn=a$selogOR^-2, a$names,
summn=a$logMH, sumse=a$selogMH, sumnn=a$selogMH^-2,
logeffect=TRUE)
```
Ultimately the two would be used for different purposes, and these examples shown are straight from the [manual](http://cran.r-project.org/web/packages/rmeta/rmeta.pdf) for [rmeta](http://cran.r-project.org/web/packages/rmeta/index.html).
It wasn't too much work to create 'another' custom option to get what my friend wanted, so thought I'd share the code for anyone else interest. There was a little bit of 'misdirection' for lack of a better term to get it across the line, and I'm sure elements of this are sloppy. You can access the code on my [github](https://github.com/nzcoops/blog_code/blob/master/forest_plot.Rmd).
I wrote this post in [RStudio](http://rstudio.org/download/) using the [R Markdown](http://www.rstudio.org/docs/authoring/using_markdown) language and then [knitr](http://yihui.name/knitr/) to turn in into markdown (.md), and then [pandoc](http://johnmacfarlane.net/pandoc/) to turn it into html. The original file is available here on [github](https://github.com/nzcoops/blog_code/blob/master/forest_plot.Rmd).
system("pandoc -s forest_plot.md -o forest_plot.html")