[R] Forestplot, grid graphics Viewplot grid.arange

Putt, Mary mputt @end|ng |rom pennmed|c|ne@upenn@edu
Mon Oct 10 02:34:07 CEST 2022


I have created several plots using the forestplot package and the link shown here. <https://cran.r-project.org/web/packages/forestplot/vignettes/forestplot.html > Great package !
Next step is to combine two plots into a single graphic. The code provided on the link results in 'bleeding' of the graphics/text into each other. I don't want to clip it as I need the text elements. I am guessing the problem involves the combination of text and graphics in the 'plot'. I fooled around with the original post and also did some hunting online but no luck Thanks in advance.
library(foresplot)
data("dfHRQoL")

#create individual forest plots for Sweden and Denmark
fp_sweden <- dfHRQoL |>
  filter(group == "Sweden") |>
  mutate(est = sprintf("%.2f", mean), .after = labeltext) |>
  forestplot(labeltext = c(labeltext, est),
             title = "Sweden",
             clip = c(-.1, Inf),
             xlab = "EQ-5D index",
             new_page = FALSE)

fp_denmark <- dfHRQoL |>
  filter(group == "Denmark") |>
  mutate(est = sprintf("%.2f", mean), .after = labeltext) |>
  forestplot(labeltext = c(labeltext, est),
             title = "Denmark",
             clip = c(-.1, Inf),
             xlab = "EQ-5D index",
             new_page = FALSE)



#now combine into a single plot using the web code; but this one bleeds into each other
library(grid)

#####
#Put plots together using grid graphics
#Attempt 1 from website

#####
grid.newpage()
borderWidth <- unit(4, "pt")
width <- unit(convertX(unit(1, "npc") - borderWidth, unitTo = "npc", valueOnly = TRUE)/2, "npc")
pushViewport(viewport(layout = grid.layout(nrow = 1,
                                           ncol = 3,
                                           widths = unit.c(width,
                                                           borderWidth,
                                                           width))
)
)
pushViewport(viewport(layout.pos.row = 1,
                      layout.pos.col = 1))
fp_sweden
upViewport()
pushViewport(viewport(layout.pos.row = 1,
                      layout.pos.col = 2))
grid.rect(gp = gpar(fill = "grey", col = "red"))
upViewport()
pushViewport(viewport(layout.pos.row = 1,
                      layout.pos.col = 3))
fp_denmark
upViewport(2)


####
#Attempt 2 from website, still a problem.
####
grid.newpage()
borderWidth <- unit(4, "pt")
width <- unit(convertX(unit(1, "npc") - borderWidth, unitTo = "npc", valueOnly = TRUE)/2, "npc")
pushViewport(viewport(layout = grid.layout(nrow = 1,
                                           ncol = 3,
                                           widths = c(0.45, 0.1, 0.45))
)
)
pushViewport(viewport(layout.pos.row = 1,
                      layout.pos.col = 1))
fp_sweden
upViewport()

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

###
#Attempt 3 converting to grobs and use patchwork
###
library(ggplotify)
library(patchwork)

fpd_grob <- grid2grob(print(fp_denmark))

p1 <- grid2grob(print(fp_denmark))
p2 <- grid2grob(print(fp_sweden))
p_both <- wrap_elements(p1) + wrap_elements(p2)
p_both

#same problem with grid.arrange()**strong text**



Mary Putt, PhD, ScD
Professor of Biostatistics
Department of Biostatistics, Epidemiology & Informatics
Pereleman School of Medicine
University of Pennsylvania

215-573-7020

	[[alternative HTML version deleted]]



More information about the R-help mailing list