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(0.05, 0.25, 1, 4)), xlim=c(-20,2),
     ilab=cbind(tpos, tneg, cpos, cneg), ilab.lab=c("TB+","TB-","TB+","TB-"),
     ilab.xpos=c(-13.5,-12,-10,-8.5), cex=0.75, header="Author(s) and Year",
     mlab="", textpos=c(-20,-3.5), shade=TRUE)
text(c(-12.75,-9.25), res$k+3, c("Vaccinated", "Control"), cex=0.75, font=2)
### 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 = ", .(fmtx(res$QE, digits=2)),
     ", df = ", .(res$k - res$p), ", ",
     .(fmtp(res$QEp, digits=3, pname="p", add0=TRUE, sep=TRUE, equal=TRUE)), "; ",
     I^2, " = ", .(fmtx(res$I2, digits=1)), "%)")))
plots/forest_plot_with_textpos.txt ยท Last modified: 2024/06/07 12:39 by Wolfgang Viechtbauer