rmetafor

Insert line break in a label of a forest plot


I am using the metafor package to build a forest plot using the code from the official page

library(metafor)
 
### copy BCG vaccine meta-analysis data into 'dat'
dat <- dat.bcg
 
### 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)
 
### a little helper function to add Q-test, I^2, and tau^2 estimate info
mlabfun <- function(text, x) {
   list(bquote(paste(.(text),
      " (Q = ", .(fmtx(x$QE, digits=2)),
      ", df = ", .(x$k - x$p), ", ",
      .(fmtp(x$QEp)), "; ",
      I^2, " = ", .(fmtx(x$I2, digits=1)), "%, ",
      tau^2, " = ", .(fmtx(x$tau2, digits=2)), ")")))}
 
### set up forest plot (with 2x2 table counts added; the 'rows' argument is
### used to specify in which rows the outcomes will be plotted)
forest(res, xlim=c(-16, 4.6), at=log(c(0.05, 0.25, 1, 4)), atransf=exp,
       ilab=cbind(tpos, tneg, cpos, cneg), ilab.lab=c("TB+","TB-","TB+","TB-"),
       ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, ylim=c(-2,28), top=4, order=alloc,
       rows=c(3:4,9:15,20:23), mlab=mlabfun("RE Model for All Studies", res),
       psize=1, header="Author(s) and Year")

I want to modify the helper function so that the three statistics (Q, I^2 and τ^2) are printed on three different lines indented to the right. The first image is what I get with the code above, the second what I am going after:

What I get

What I want

I have tried atop(), paste with sep ="/n" among other things but to no avail.

Any help appreciated!


Solution

  • The easiest would be to just add each line separately. Maybe something like this:

    forest(res, xlim=c(-16, 4.6), at=log(c(0.05, 0.25, 1, 4)), atransf=exp,
           ilab=cbind(tpos, tneg, cpos, cneg), ilab.lab=c("TB+","TB-","TB+","TB-"),
           ilab.xpos=c(-9.5,-8,-6,-4.5), cex=0.75, ylim=c(-2,28), top=4, order=alloc,
           rows=c(3:4,9:15,20:23), mlab="RE Model for All Studies",
           psize=1, header="Author(s) and Year")
    
    par(xpd=NA)
    text(-16, -1.8, pos=4, cex=0.75, bquote(paste(
          "Q = ", .(fmtx(res$QE, digits=2)), ", df = ", .(res$k - res$p), ", ", .(fmtp2(res$QEp)))))
    text(-16, -2.6, pos=4, cex=0.75, bquote(paste(
          I^2, " = ", .(fmtx(res$I2, digits=1)), "%")))
    text(-16, -3.4, pos=4, cex=0.75, bquote(paste(
          tau^2, " = ", .(fmtx(res$tau2, digits=2)))))
    par(xpd=FALSE)