[R-meta] Two large forest plots in one image
Emanuel Schembri
|e||d26 @end|ng |rom gm@||@com
Sun Aug 25 22:40:59 CEST 2024
Dear Guido,
Apologies for the late reply.
I have been trying to work it out with your code, but unfortunately,
it was not coming out nicely.
However, I managed to use the {magik} package to work around it with
visually appealing images.
Kindest regards,
Emanuel
Below is the code:
Save each forest plot using the Plots -> Export -> Save as Image.
Save each forest plot in png format using a constant width of 950 and
an appropriate height so they fit well.
library(magick)
# Load the images
img1 <- image_read("image_1.png")
img2 <- image_read("image_2.png")
img3 <- image_read("image_3.png")
# Annotate each image with a letter
img1_annotated <- image_annotate(img1, "A", size = 40, gravity =
"northwest", color = "black")
img2_annotated <- image_annotate(img2, "B", size = 40, gravity =
"northwest", color = "black")
img3_annotated <- image_annotate(img3, "C", size = 40, gravity =
"northwest", color = "black")
# Display the annotated images (optional)
img1_annotated
img2_annotated
img3_annotated
# Combine images into a grid
combined_image <- image_append(c(img1_annotated,
img2_annotated,img3_annotated), stack = TRUE)
# add extra white space around the combined plot
padded_legend <- image_border(combined_image, color = "white",
geometry = "50x20")
padded_legend
# Save the combined image
image_write(padded_legend, path = "combined_plots_123.png")
On Mon, Jul 29, 2024 at 5:49 PM Dr. Guido Schwarzer
<guido.schwarzer using uniklinik-freiburg.de> wrote:
>
> 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