The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


Table of Contents

Forest Plot with Superscripts


Some journals require reference numbers to be shown in forest plots (in addition to the study authors and publication years). Typically, the reference numbers are supposed to be shown as superscripts. How to accomplish this with R and the metafor package has occasionally been asked. Text involving superscripts can be added to plots using 'expressions', which are more generally used to add mathematical equations to plots. See help(plotmath) for the syntax to create such mathematical expressions. An additional difficulty in the present context is that one typically wants to substitute variables (like the reference numbers) into these expressions. This can be accomplished in various ways using functions such as substitute() and bquote(). Below is an example to illustrate how this can be done to add reference numbers as superscripts and additional information from the random-effects model to a forest plot.



### copy BCG vaccine meta-analysis data into 'dat'
dat <-
### calculate log risk ratios and corresponding sampling variances
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat)
### fit random-effects model
res <- rma(yi, vi, data=dat)
### a little helper function to add Q-test, I^2, and tau^2 estimate info
mlabfun <- function(text, x) {
      " (Q = ", .(formatC(x$QE, digits=2, format="f")),
      ", df = ", .(x$k - x$p),
      ", p ", .(metafor:::.pval(x$QEp, digits=2, showeq=TRUE, sep=" ")), "; ",
      I^2, " = ", .(formatC(x$I2, digits=1, format="f")), "%, ",
      tau^2, " = ", .(formatC(x$tau2, digits=2, format="f")), ")")))}
### create study labels
dat$slab <- with(dat, paste(author, year, sep=", "))
### combine study labels with trial numbers in superscripts
dat$slab.ref <- mapply(function(x,y) as.expression(bquote(.(x)^.(y))), dat$slab, dat$trial)
### forest plot with extra annotations
forest(res, atransf=exp, at=log(c(.05, .25, 1, 4)), xlim=c(-16,6),
       ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.75, header="Author(s) and Year",
       mlab=mlabfun("RE Model", res), slab=dat$slab.ref)
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"))
plots/forest_plot_with_superscripts.txt ยท Last modified: 2021/11/15 14:19 by Wolfgang Viechtbauer

Page Tools