[R-meta] Two large forest plots in one image

Dr. Guido Schwarzer gu|do@@chw@rzer @end|ng |rom un|k||n|k-|re|burg@de
Mon Jul 29 17:49:51 CEST 2024


Emanuel,

I typically generate a PDF-file using pushViewport() from the grid package. The code below works for two forest plots either side by side or one below the other.

Best,
Guido


library("meta")
library("grid")

E1 <- structure(list(study = c("Barratt", "Carli", "Gupta", "Taqi",
"Chen", "Hübner", "Zutshi", "Park", "Klotz", "Mohamad", "Chen",
"Tsui"), year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000), ev.EZ = c(1, 3, 1, 1, 0, 2, 0, 1, 1,
0, 0, 0), n.EZ = c(55, 41, 60, 9, 26, 65, 31, 25, 21, 28, 25,
57), ev.PPA = c(1, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0), n.PPA = c(55,
42, 60, 11, 27, 57, 28, 22, 21, 28, 25, 54), TRX = c(1, 2, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2), TIME = c("Update", "Update", "Update",
"Old", "Old", "Old", "Old", "Old", "Old", "Old", "Old", "Old"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-12L))


E2 <- structure(list(study = c("Steinberg", "Parker", "Madej", "Schumann",
"Carli", "Tsui", "Xu", "Park", "Cho", "Xu", "Yosunkaya", "Mann",
"Jayr", "Aceto"), year = c(2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000), ev.EZ = c(8,
3, 6, 16, 10, 10, 4, 11, 7, 5, 16, 30, 16, 2), n.EZ = c(51, 55,
41, 60, 20, 31, 20, 51, 20, 36, 21, 57, 38, 20), ev.PPA = c(9,
11, 8, 27, 11, 10, 3, 3, 9, 11, 17, 29, 14, 0), n.PPA = c(51,
55, 42, 60, 20, 33, 21, 48, 10, 32, 21, 54, 46, 19), TRX = c(2,
1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2), TIME = c("Update", "Update",
"Update", "Update", "Old", "Old", "Old", "Old", "Old", "Old",
"Old", "Old", "Old", "Old")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -14L))


M3 <-  metabin(ev.EZ, n.EZ, ev.PPA, n.PPA,
               data = E1,
               studlab = paste0(study, " (", year, ")"),
               sm = "RR",
               method = "MH",
               subgroup = TIME)

M4 <- metabin(ev.EZ, n.EZ, ev.PPA, n.PPA,
              data = E2,
              studlab = paste0(study, " (", year, ")"),
              sm = "RR",
              method = "MH",
              subgroup = TRX)


height <- 14
width <- 9
#
ncols <- 1
nrows <- 2
#
firstrow <- 0.5
firstcol <- 0.5

pdf("two-forestplots.pdf", width = width, height = height)

pushViewport(
  viewport(
    layout = grid.layout(
    nrow = nrows, ncol = ncols,
    widths = unit(2 * c(firstcol, 1 - firstcol) * width / ncols,
                  "inches"),
    heights = unit(2 * c(firstrow, 1 - firstrow) * height / nrows,
                  "inches"))
  )
  )

pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))

forest(M3, new = FALSE,
       common = FALSE,
       test.overall.random=TRUE,
       prediction=TRUE,
       print.Q=TRUE,
       print.Q.pval=TRUE,
       just = "right",
       layout = "RevMan5",
       pooled.events=TRUE,
       sortvar = year)

grid.text("Figure A", 0.025, 0.96, gp = grid::gpar(cex = 1.5), just = "left")

popViewport()

pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 2))

forest(M4, new = FALSE,
       common = FALSE,
       test.overall.random=TRUE,
       prediction=TRUE,
       print.Q=TRUE,
       print.Q.pval=TRUE,
       just = "right",
       layout = "RevMan5",
       pooled.events=TRUE,
       sortvar = year)

grid.text("Figure B", 0.025, 0.98, gp = grid::gpar(cex = 1.5), just = "left")

popViewport()

dev.off()



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