我正在使用 metafor 包使用官方页面中的代码构建森林图
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")
我想修改辅助函数,以便将三个统计数据(Q、I^2 和 τ^2)打印在向右缩进的三个不同行上。第一张图片是我通过上面的代码得到的,第二张图片是我想要的:
我尝试过 atop()、使用 sep ="/n" 进行粘贴,但没有成功。
任何帮助表示赞赏!
最简单的方法是单独添加每一行。也许是这样的:
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)