[R-meta] question on forest plot formatring

Viechtbauer, Wolfgang (NP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Mon Jun 12 13:29:15 CEST 2023


Dear Gabriel,

Maybe something like this?

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, res) {
   list(bquote(paste(.(text),
      "Q = ", .(formatC(res$QE, digits=2, format="f")),
      ", df = ", .(res$k - res$p),
      ", p ", .(metafor:::.pval(res$QEp, digits=2, showeq=TRUE, sep=" ")), "; ",
      I^2, " = ", .(formatC(res$I2, digits=1, format="f")), "%, ",
      tau^2, " = ", .(formatC(res$tau2, digits=2, format="f")))))}
 
### 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.xpos=c(-9.5,-8,-6,-4.5),
       cex=0.75, ylim=c(-1, 27), order=alloc, rows=c(3:4,9:15,20:23),
       mlab="RE Model for All Studies",
       psize=1, header="Author(s) and Year")
text(-16, -1.8, mlabfun("", res)[[1]], pos=4, cex=0.75)

Modify this as needed.

As for the weight column -- if you want to place it somewhere else, I would suggest to just include the weights (which you can obtain via weights()) via 'ilab'. Then you have full control (via 'ilab.xpos') where to place the values.

Best,
Wolfgang

>-----Original Message-----
>From: R-sig-meta-analysis [mailto:r-sig-meta-analysis-bounces using r-project.org] On
>Behalf Of Michael Dewey via R-sig-meta-analysis
>Sent: Saturday, 10 June, 2023 16:07
>To: Gabriel Cotlier
>Cc: Michael Dewey; R Special Interest Group for Meta-Analysis
>Subject: Re: [R-meta] question on forest plot formatring
>
>Since presumably my two original suggestions did not work I think I will
>leave this to someone more expert with metafor graphics.
>
>Michael
>
>On 10/06/2023 07:01, Gabriel Cotlier wrote:
>> Hello Michael,
>>
>> Thanks a lot for your response.
>>
>> Since I have a lot of lines in my forest plot (with a large number of
>> correlations ) therefore I am using a small font size in order to make
>> the summary of the model's results line more readable, I thought that
>> maybe it is possible to print in the plot the model's summary results in
>> two lines instead of one, just as a possible option...? Since I
>> mentioned I was using this example as base code:
>> https://www.metafor-project.org/doku.php/plots:forest_plot_with_subgroups
><https://www.metafor-project.org/doku.php/plots:forest_plot_with_subgroups> . I
>think maybe there exist some parameters in the function "mlabfun" could be a
>possible solution?
>>
>> Regarding spacing the columns of at right hand side of the plot that is
>> "weight %" and CI fisher's z" I could not find a parameter to space them
>> a tab to make the plot more redabel.
>>
>> Thanks a lot.
>> Kind regards,
>> Gabriel


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