# [R-meta] Question on Forest Plot by subgroups

Gabriel Cotlier g@b|k|m01 @end|ng |rom gm@||@com
Mon Jun 19 16:49:37 CEST 2023

```Hello all,

I found the solution to the problem to the code I plotted previously. The
problem was that I was ordering by "alloc" which is a column of sing() of
correlations and not by the column that divide the groups (3 groups
expressed in a column named "Type"  : Computational
simulation","Experimental","Natural Rainfed") which in my case is named
"Type",after correcting this I also remove unnecessary redundant
information subset=(alloc == 1) in each of the models res.s, res.r and
res.a. Please find the corrected working code below with highlighted
locations where modifications were made  :

############################## CODE
###########################################

pdf(file="ForestPlotCompleteDataSetRahul_Test.pdf", width = 12, height = 35)

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")), ")")))}

forest(res,
top = 2,
#xlim=c(-8, 6),
#alim = c(-3.36077, 5.181815),
alim = c(-3.8, 6),
#at=log(c(0.05, 0.25, 1, 4)),
#atransf=exp,
#width = 4,
showweights = TRUE,
ilab=cbind(ni),  # content of extra columns of information
ilab.xpos=c(-4.6), # position of extra columns of information
cex=0.75,
ylim=c(-1, 161), # rows in the y-axis from min-row to max-row with
text.
slab = paste(dat\$Authors, dat\$Year, sep = ", "),
order=Type,
rows=c(2:15,19:85,89:156), # this divide by groups the studies (3
groups)
mlab=mlabfun("RE Model for All Studies", res),
#psize= 1 ,
efac = 0.2, # size of the polygon (the diamond)
)
# abline(h=res\$k+8, lwd=2, col="white")

op <- par(cex=0.75, font=2)

text(c(-15), res\$k+10,  c("Author(s) and Year"), cex = 1.2, font = 2)
text(c(-4.6), res\$k+10,  c("Samples size"), cex = 1.2, font = 2)
text(c(9.7),  res\$k+10,  c("Weight"),cex = 1.2, font=2)
text(c(9.7),  res\$k+9.3, c("%"),cex = 1.2, font=2)
text(c(12.6),  res\$k+10,  c("Fisher's zr [95% CI]"),cex = 1.2, font=2)
text(c(res\$k+13), c("Complete Dataset"), cex = 2, font = 2)

par(font=4)

### add text for the subgroups
text(-17.2, c(16,86,157), pos=4, c("Computational simulation",
"Experimental",
"Natural Rainfed"), cex = 1.3)

par(op)

### fit random-effects model in the three subgroups
res.s <- rma(yi, vi, data=dat)
res.r <- rma(yi, vi, data=dat)
res.a <- rma(yi, vi,  data=dat)

### add summary polygons for the three subgroups
addpoly(res.s, row= 88, mlab=mlabfun("RE Model for Subgroup", res.s),efac =
0.2, col='red')
addpoly(res.r, row= 18, mlab=mlabfun("RE Model for Subgroup", res.r),efac =
0.2, col='green')
addpoly(res.a, row= 1, mlab=mlabfun("RE Model for Subgroup", res.a),efac =
0.2, col='blue')

dev.off()

On Mon, Jun 19, 2023 at 3:46 PM Gabriel Cotlier <gabiklm01 using gmail.com> wrote:

> Hello all,
>
> I have taken an example from here
> <https://www.metafor-project.org/doku.php/plots:forest_plot_with_subgroups> and
> modified it to my case study of Fisher's z correlations meta-analysis with
> the code below.
> I would like to plot a forest plot in which each of the subgroups ( 3
> groups expressed in a column named "Type"  : Computational
> simulation","Experimental","Natural Rainfed") each showing the studies
> (rows) with positive correlations and those with negative correlations
> divided in agreement to the line of no effect located at zero. I have used
> a column named "alloc" which has a 1 for those correlations (rows) with
> positive value and -1 for those with negative value, however for some
> reason it does not work as expected. It mixes the lines from
> different groups, ploting them where they do not correspond and also does
> not plot the model summary for the second and third group.
> I tried to change several arguments to solve it, however unsuccessfully.
> What could be the error?
> Thanks a lot for your help.
>
> ############################## CODE
> ###########################################
>
> pdf(file="ForestPlotCompleteDataSetbyGroups.pdf", width = 12, height = 35)
>
> 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")), ")")))}
> forest(res,
>        top = 2,
>        # xlim=c(-8, 6),
>        # alim = c(-3.36077, 5.181815),
>        alim = c(-3.8, 6),
>        # at=log(c(0.05, 0.25, 1, 4)),
>        # atransf=exp,
>        # width = 4,
>        showweights = TRUE,
>        ilab=cbind(ni),  # content of extra columns of information
>        ilab.xpos=c(-4.6), # position of extra columns of information
>        cex=0.75,
>        ylim=c(-1, 161), # rows in the y-axis from min-row to max-row with
> text.
>        slab = paste(dat\$Authors, dat\$Year, sep = ", "),
>        order=alloc,
>        rows=c(2:15,19:85,89:156), # this divide by groups the studies (3
> groups) from column named "Type"
>        mlab=mlabfun("RE Model for All Studies", res),
>        #psize= 1 ,
>        efac = 0.2, # size of the polygon (the diamond)
>        )
>
> op <- par(cex=0.75, font=2)
>
> text(c(-15), res\$k+10,  c("Author(s) and Year"), cex = 1.2, font = 2)
> text(c(-4.6), res\$k+10,  c("Samples size"), cex = 1.2, font = 2)
> text(c(9.7),  res\$k+10,  c("Weight"),cex = 1.2, font=2)
> text(c(9.7),  res\$k+9.3, c("%"),cex = 1.2, font=2)
> text(c(12.6),  res\$k+10,  c("Fisher's zr [95% CI]"),cex = 1.2, font=2)
> text(c(res\$k+13), c("Complete Dataset"), cex = 2, font = 2)
>
> par(font=4)
>
> text(-17.2, c(16,86,157), pos=4, c("Computational simulation",
>                                  "Experimental",
>                                  "Natural Rainfed"), cex = 1.3)
> par(op)
>
> ### fit random-effects model in the three subgroups
> res.s <- rma(yi, vi, subset=(alloc == 1), data=dat)
> res.r <- rma(yi, vi, subset=(alloc == 2), data=dat)
> res.a <- rma(yi, vi, subset=(alloc == 3),  data=dat)
>
> ### add summary polygons for the three subgroups
> addpoly(res.s, row= 88, mlab=mlabfun("RE Model for Subgroup", res.s),efac
> = 0.2, col='red')
> addpoly(res.r, row= 18, mlab=mlabfun("RE Model for Subgroup", res.r),efac
> = 0.2, col='green')
> addpoly(res.a, row= 1, mlab=mlabfun("RE Model for Subgroup", res.a),efac =
> 0.2, col='blue')
>
> dev.off()
>

[[alternative HTML version deleted]]

```