[R] tweaking forest plot (metafor package)

Viechtbauer Wolfgang (STAT) wolfgang.viechtbauer at maastrichtuniversity.nl
Thu Jul 19 11:12:10 CEST 2012

I think Michael already gave you some really good hints. To add a bit more details to this, first of all, I suggest to use:

q2 <- rma(yi, vi, mods=cbind(grupo), data=qim)
forest(q2, transf=transf.ztor, digits=3, alim=c(0,1), refline=.5, addfit=FALSE)

so that the fitted values for that categorical moderator are not actually shown in the plot yet. Then use:


after you create the forest plot. That will tell you what the xlim values are that were chosen by default (the first and second value). Start adjusting these values with the xlim argument to see how that expands and shrinks the horizontal space.

Next, try the following. Suppose you have k=20 observed correlations in total that will be shown in the forest plot. Then the default value for ylim (for a model that does includes a moderator, as you have) will be ylim=c(0.5, 23) (i.e., the second value is k+3). The first study will be placed in row 20 of the plot, the second in row 19, and so on, all the way down to row 1 for the 20th study. So, try this:

abline(h=20, lwd=3)
abline(h=1, lwd=3)

which will show you where that first and last study are being placed.

In row 21, the horizontal line is plotted. So try this:

abline(h=20, lwd=3)

which shows you where that line is being placed.

Rows 22 and 23 are for some extra white space at the top of the figure to add column headings (with the text() function). So try this:

text(par("usr")[1], 22, "Study", pos=4, font=2)
text(par("usr")[2], 22, "Cor [95% CI]", pos=2, font=2)

which will add column headings for the first and last column. Of course, you can adjust the value 22 to something slightly different (i.e., to adjust the vertical placement of the headings).

Now try using ylim=c(0.5, 30) in the call to forest(). You will see that a bunch of extra vertical space is added. The first study is still placed in row 20, going down again to row 1 for the 20th study. Confirm this with:

abline(h=20, lwd=3)
abline(h=1, lwd=3)

Now, you want to use the row argument to actually tell the forest() function where those studies should be placed. Try rows=c(25:21, 15:1) in the call to forest(), which will place study 1 in row 25, study 2 in row 24, going down to study 5 in row 21, and then study 6 in row 15, study 7 in row 14, going down to study 1 in row 1. Again, you can use something like:

abline(h=25, lwd=3)
abline(h=15, lwd=3)

to check on the vertical placement of the 1st and 6th study.

So now you have full control over the placement of the studies and you can add space as needed. Then you can use the addpoly() function to add summary polygons for the various levels of the "grupo" factor. I suggest carefully going through the last example in:


to see a detailed example of this.

Finally, to change the axis limits to .5 and 1, just use alim=c(0.5, 1) in the call to forest().

I hope this helps!



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) 388-4170 | http://www.wvbauer.com   

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Michael Dewey
> Sent: Wednesday, July 18, 2012 19:16
> To: eesteves at ualg.pt; r-help at r-project.org
> Subject: Re: [R] tweaking forest plot (metafor package)
> At 00:10 18/07/2012, eesteves at ualg.pt wrote:
> >Dear All,
> >
> >I'm having trouble tweaking a forest plot made using the R
> >meta-analysis package metafor. I did the analysis based upon the
> >correlation coeff from studies and plotted the corresponding forest
> >plot easily
> >
> >>q2<-rma(yi,vi,mods=cbind(grupo),data=qim)
> >>q2
> >>forest (q2,transf=transf.ztor,digits=3, ... ,alim=c(0,1),refline=.5)
> >>text(-1.55,42,"Espécie [refª]",pos=4,font=2)
> Not sure why you use cbind(grupo) but it will not change much.
> >I'd like to subdivide the "table" by the moderator 'grupo' - i.e.
> >create the necessary spacings in the list of studies to accommodate
> >the moderator statistics (cf.
> >http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=metafor:addpoly.default).
> >
> >My main issue is that I cannot really understand how the function
> >(forest()) really uses space; specifically, how to use the parameters
> >'xlim', 'ylim' and 'rows' in the function? how can I change the scale
> >from 0-1 to 0.5-1?
> An example of what went wrong would help but I
> think you will find that the rows argument tells
> forest where to put the results of the individual
> trials so if you give it non-consecutive integers
> you will get gaps in the plot which you can fill
> with addpoly. So if rows does not contain 4 you
> can use addpoly to put something in row 4. As far
> as xlim and ylim are concerned my advice would be
> to try a whole range of values and see what
> happens as this is one of those cases where it is
> easier to experiment than to explain. If all else
> fails either (a) look at the code (b) wait for
> Wolfgang to ell you and me what really happens.
> >Hopefully I've made myself (relatively) clear but... Thanks.
> >
> >Eduardo Esteves
> >
> >
> Michael Dewey
> info at aghmed.fsnet.co.uk
> http://www.aghmed.fsnet.co.uk/home.html

More information about the R-help mailing list