Jokel Meyer
2012-Jul-24 14:15 UTC
[R] Annotate forest plot 'forest.rma()' for meta-analysis with metafor package
Dear R-experts,
The forest.rma() function from the metafor package creates nice forest
plots for presenting the results of a meta-analysis. These plots can be
annotated for e.g. giving names to the columns. E.g. as in the
documentation of the package:
data(dat.bcg)
### meta-analysis of the log relative risks using a random-effects model
res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
measure="RR",
slab=paste(author, year, sep=", "),
method="REML")
forest(res, slab=paste(dat.bcg$author, dat.bcg$year, sep=",
"),
xlim=c(-16, 6), at=log(c(.05, .25, 1, 4)), atransf=exp,
ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),
ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.75)
op <- par(cex=.75, font=2)
text(c(-9.5,-8,-6,-4.5), 15, c("TB+", "TB-",
"TB+", "TB-"))
text(c(-8.75,-5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos=4)
text(6, 15, "Relative Risk [95% CI]", pos=2)
par(op)
However the column names have to be re-positioned everytime the number of
included studies in the meta-analysis changes.
E.g.:
dat.bcg <- dat.bcg[1:12,]
### meta-analysis of the log relative risks using a random-effects model
res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
measure="RR",
slab=paste(author, year, sep=", "),
method="REML")
forest(res, slab=paste(dat.bcg$author, dat.bcg$year, sep=", "),
xlim=c(-16, 6), at=log(c(.05, .25, 1, 4)), atransf=exp,
ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),
ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.75)
op <- par(cex=.75, font=2)
text(c(-9.5,-8,-6,-4.5), 15, c("TB+", "TB-",
"TB+", "TB-"))
text(c(-8.75,-5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos=4)
text(6, 15, "Relative Risk [95% CI]", pos=2)
par(op)
Is there a way to define the colum names' position relatively to the plot
so they would be adequately positioned no matter what is the number of
studies included in the plot?
Many thanks for your help!,
Jokel
[[alternative HTML version deleted]]
Viechtbauer Wolfgang (STAT)
2012-Jul-25 07:50 UTC
[R] Annotate forest plot 'forest.rma()' for meta-analysis with metafor package
Just replace the fixed y coordinate values in the text() calls with something
that will change appropriately with the number of studies/effects included in
the plot. For example:
text(c(-9.5,-8,-6,-4.5), res$k+2, c("TB+", "TB-",
"TB+", "TB-"))
text(c(-8.75,-5.25), res$k+3, c("Vaccinated",
"Control"))
text(-16, res$k+2, "Author(s) and Year", pos=4)
text(6, res$k+2, "Relative Risk [95% CI]", pos=2)
should do it.
Best,
Wolfgang
--
Wolfgang Viechtbauer, Ph.D., Statistician
Department of Psychiatry and Psychology
School for Mental Health and Neuroscience
Faculty of Health, Medicine, and Life Sciences
Maastricht University, P.O. Box 616 (VIJV1)
6200 MD Maastricht, The Netherlands
+31 (43) 388-4170 | http://www.wvbauer.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
> On Behalf Of Jokel Meyer
> Sent: Tuesday, July 24, 2012 16:16
> To: r-help at r-project.org
> Subject: [R] Annotate forest plot 'forest.rma()' for meta-analysis
with
> metafor package
>
> Dear R-experts,
>
> The forest.rma() function from the metafor package creates nice forest
> plots for presenting the results of a meta-analysis. These plots can be
> annotated for e.g. giving names to the columns. E.g. as in the
> documentation of the package:
>
> data(dat.bcg)
>
> ### meta-analysis of the log relative risks using a random-effects model
> res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
measure="RR",
> slab=paste(author, year, sep=", "),
method="REML")
>
> forest(res, slab=paste(dat.bcg$author, dat.bcg$year, sep=",
"),
> xlim=c(-16, 6), at=log(c(.05, .25, 1, 4)), atransf=exp,
> ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),
> ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.75)
> op <- par(cex=.75, font=2)
> text(c(-9.5,-8,-6,-4.5), 15, c("TB+", "TB-",
"TB+", "TB-"))
> text(c(-8.75,-5.25), 16, c("Vaccinated",
"Control"))
> text(-16, 15, "Author(s) and Year", pos=4)
> text(6, 15, "Relative Risk [95% CI]", pos=2)
> par(op)
>
>
> However the column names have to be re-positioned everytime the number of
> included studies in the meta-analysis changes.
> E.g.:
>
> dat.bcg <- dat.bcg[1:12,]
>
> ### meta-analysis of the log relative risks using a random-effects model
> res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
measure="RR",
> slab=paste(author, year, sep=", "),
method="REML")
>
> forest(res, slab=paste(dat.bcg$author, dat.bcg$year, sep=", "),
> xlim=c(-16, 6), at=log(c(.05, .25, 1, 4)), atransf=exp,
> ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg),
> ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.75)
> op <- par(cex=.75, font=2)
> text(c(-9.5,-8,-6,-4.5), 15, c("TB+", "TB-",
"TB+", "TB-"))
> text(c(-8.75,-5.25), 16, c("Vaccinated",
"Control"))
> text(-16, 15, "Author(s) and Year", pos=4)
> text(6, 15, "Relative Risk [95% CI]", pos=2)
> par(op)
>
> Is there a way to define the colum names' position relatively to the
plot
> so they would be adequately positioned no matter what is the number of
> studies included in the plot?
>
>
> Many thanks for your help!,
> Jokel