Skip to content
Prev 4679 / 5636 Next

[R-meta] question on forest plot formatring

Dear Gabriel,

Maybe something like this?

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, res) {
   list(bquote(paste(.(text),
      "Q = ", .(formatC(res$QE, digits=2, format="f")),
      ", df = ", .(res$k - res$p),
      ", p ", .(metafor:::.pval(res$QEp, digits=2, showeq=TRUE, sep=" ")), "; ",
      I^2, " = ", .(formatC(res$I2, digits=1, format="f")), "%, ",
      tau^2, " = ", .(formatC(res$tau2, digits=2, format="f")))))}
 
### 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.xpos=c(-9.5,-8,-6,-4.5),
       cex=0.75, ylim=c(-1, 27), order=alloc, rows=c(3:4,9:15,20:23),
       mlab="RE Model for All Studies",
       psize=1, header="Author(s) and Year")
text(-16, -1.8, mlabfun("", res)[[1]], pos=4, cex=0.75)

Modify this as needed.

As for the weight column -- if you want to place it somewhere else, I would suggest to just include the weights (which you can obtain via weights()) via 'ilab'. Then you have full control (via 'ilab.xpos') where to place the values.

Best,
Wolfgang