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

Viechtbauer, Wolfgang (SP) wolfg@ng@viechtb@uer @ending from m@@@trichtuniver@ity@nl
Thu May 24 20:44:29 CEST 2018

Dear Moritz,

Thanks for the code. Unfortunately, it doesn't work for me. One problem are the curly quotes (“) that R won't like. Also, 'sizex' needs to be defined first. But after fixing that, this can't work. If I run:

ggplot(plot.dat, aes(ablat, exp(yi)))
+ geom_point(pch = 19, cex = 1.5 * sizex)

then this is interpreted as two separate commands. This would work:

ggplot(plot.dat, aes(ablat, exp(yi))) +
geom_point(pch = 19, cex = 1.5 * sizex)

Then I also got:

Error in log_trans() : could not find function "log_trans"

At that point, I kind of gave up.


-----Original Message-----
From: R-sig-meta-analysis [mailto:r-sig-meta-analysis-bounces using r-project.org] On Behalf Of Moritz Tobiasch
Sent: Tuesday, 22 May, 2018 23:01
To: r-sig-meta-analysis using r-project.org
Subject: [R-meta] meta-regression scatterplot in ggplot2: code

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 using gmail.com

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