The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


Table of Contents

Forest Plot with Adjusted Text Position


By default, the forest() function places the annotations (giving the study-specific estimates and corresponding confidence interval bounds) to the right of the actual forest plot. See here for an example. Sometimes, we might prefer to show those annotations in a different position. This can be done with the textpos argument, which takes as input a vector of length 2 to specify the placement of the study labels and the annotations. The plot below illustrates the use of this argument.



### copy BCG vaccine meta-analysis data into 'dat'
dat <-
### calculate log risk ratios and corresponding sampling variances (and use
### the 'slab' argument to store study labels as part of the data frame)
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat,
              slab=paste(author, year, sep=", "))
### fit random-effects model
res <- rma(yi, vi, data=dat)
### forest plot with extra annotations
forest(res, atransf=exp, at=log(c(.05, .25, 1, 4)), xlim=c(-20,2),
     ilab=cbind(tpos, tneg, cpos, cneg), ilab.xpos=c(-13.5,-12,-10,-8.5),
     cex=.75, header="Author(s) and Year", mlab="", textpos=c(-20,-3.5))
op <- par(cex=.75, font=2)
text(c(-13.5,-12,-10,-8.5), 15, c("TB+", "TB-", "TB+", "TB-"))
text(c(-12.75,-9.25),     16, c("Vaccinated", "Control"))
### add text with Q-value, dfs, p-value, and I^2 statistic
text(-20, -1, pos=4, cex=0.75, bquote(paste("RE Model (Q = ",
   .(formatC(res$QE, digits=2, format="f")), ", df = ", .(res$k - res$p),
   ", p = ", .(formatC(res$QEp, digits=2, format="f")), "; ", I^2, " = ",
   .(formatC(res$I2, digits=1, format="f")), "%)")))
plots/forest_plot_with_textpos.txt ยท Last modified: 2022/06/13 13:12 by Wolfgang Viechtbauer

Page Tools