[R-meta] meta-regression scatterplot in ggplot2: code

Moritz Tobiasch moritztobi@@ch @ending from gm@il@com
Tue May 22 23:01:01 CEST 2018

Dear metafor community,

I recently had the very honour to dig through R code in order to set up a meta-regression scatterplot in ggplot2. As this experience was … interesting for me, but doesn’t warrant to be repeated by anyone else, I allow myself to send my approach to this honorable mailing list, for further use as deemed appropriate. @Wolfgang Viechtbauer: of course you may add this code (if it seems worth it) to your code examples on www.metafor-project.com.

Calculations are closely based on and expand the example of a meta-analytic scatterplot as shown on the website http://www.metafor-project.org/doku.php/plots:meta_analytic_scatterplot

In order to gain access to all necessary parameters, confidence interval boundaries are extracted from the escalc() function by
plot.dat <- summary.escalc(dat)
which generates a data.table() with effect size yi, variance vi, standard error and confidence interval boundaries ci.lb and ci.ub.

The data can then be put in the ggplot() syntax as following:

ggplot(plot.dat, aes(ablat, exp(yi)))
+ geom_point(pch = 19, cex = 1.5 * sizex) # as, to my experience, ggplot() will render dots smaller than the plot() function
+ geom_linerange(aes(ymin = exp(plot.dat$ci.lb), ymax = exp(plot.dat$ci.ub)), size = .25) # gives straight lines w/o brackets at the ends. if „classical“ look is preferred, use geom_errorbar()
+ scale_y_continuous(trans = log_trans(), limits = c(0.2, 1.6)) # for adjustment of axes, see (as example) http://www.cookbook-r.com/Graphs/
+ xlab("Absolute Latitude“)
+ ylab("risk ratio“)
+ geom_hline(yintercept = 1, linetype = "dotted“) # draws the dotted line at y = 1 for equal risk ratio
+ geom_line(data = preds, aes(x = preds$ablat, y = preds$pred)) # plots the regression line
+ geom_line(data = preds, aes(x = preds$ablat, y = preds$ci.lb), linetype = "dashed“) # plots the lower confidence interval boundary
+ geom_line(data = preds, aes(x = preds$ablat, y = preds$ci.ub), linetype = "dashed“) # plots the upper confidence interval boundary

Labeling of individual data points was not in the scope of my work, but could be done by the geom_text(aes(label = label)) command.

Hope this helps.


Dr. med. Moritz Tobiasch

Kirchgasse 22b
A-6020 Innsbruck

Tel.: +43 650 5667954
moritztobiasch at gmail.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-meta-analysis/attachments/20180522/befcf049/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 873 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://stat.ethz.ch/pipermail/r-sig-meta-analysis/attachments/20180522/befcf049/attachment.sig>

More information about the R-sig-meta-analysis mailing list