Skip to content
Prev 280120 / 398506 Next

Overlaying density plot on forest plot

Here is an example. It requires a bit of extra work, but that's the beauty of R -- you can essentially customize a graph to your taste with low-level functions. I also added the prediction interval using the addcred argument (as suggested by Michael), since it gives the same information as that fancy normal distribution at the bottom. Essentially, that distribution is just window-dressing. I am not sure if I would go as far as calling that "chart-junk", but it's certainly somewhat gratuitous. Well, I hope the code below gives you enough of an idea so that you can adjust this to your specific case.


library(metafor)

### load BCG vaccine data
data(dat.bcg)

### meta-analysis of the log relative risks using a random-effects model
res <- rma(ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg, measure="RR", 
           slab=paste(author, year, sep=", "), method="REML")

### forest plot with extra annotations
forest(res, slab=paste(dat.bcg$author, dat.bcg$year, sep=", "), 
       xlim=c(-16, 6), at=log(c(.05, .25, 1, 4)), atransf=exp,
       ilab=cbind(dat.bcg$tpos, dat.bcg$tneg, dat.bcg$cpos, dat.bcg$cneg), 
       ilab.xpos=c(-9.5,-8,-6,-4.5), cex=.7, ylim=c(-3,16), addcred=TRUE)
text(c(-9.5,-8,-6,-4.5), 15, c("TB+", "TB-", "TB+", "TB-"), font=2, cex=.70)
text(c(-8.75,-5.25),     16, c("Vaccinated", "Control"), font=2, cex=.70)
text(-16,                15, "Author(s) and Year",     pos=4, font=2, cex=.70)
text(6,                  15, "Relative Risk [95% CI]", pos=2, font=2, cex=.70)
text(-16,              -2.5, "Prediction Distribution", pos=4, cex=.70)

### add prediction distribution
res <- predict(res)
pred.m <- res$pred
pred.s <- (res$cr.ub - res$cr.lb) / (2*1.96)
xs <- seq(-2.5,1,.01)
cords.xs <- c(-2,xs,-2)
cords.ys <- c(0, 1.5*dnorm(xs, pred.m, sd=pred.s), 0) - 3
polygon(cords.xs, cords.ys, col="darkgray", border=NA)
ptext <- paste(round(exp(res$pred),2), " [ ", round(exp(res$cr.lb),2), " , ", round(exp(res$cr.ub),2), "]", sep="")
text(6, -2.5, ptext, pos=2, cex=.70)

--   
Wolfgang Viechtbauer, Ph.D., Statistician   
Department of Psychiatry and Psychology   
School for Mental Health and Neuroscience   
Faculty of Health, Medicine, and Life Sciences   
Maastricht University, P.O. Box 616 (VIJV1)   
6200 MD Maastricht, The Netherlands   
+31 (43) 368-5248 | http://www.wvbauer.com