Grid.arrange 个森林图对象

Grid.arrange forestplot objects

我有几个使用 R 包制作的森林图 forestplot。我想 grid.arrange 他们 - 但似乎这并不容易。

示例:

library(forestplot)
# A basic example, create some fake data
row_names <- list(list("test = 1", expression(test >= 2)))
test_data <- data.frame(coef=c(1.59, 1.24),
                    low=c(1.4, 0.78),
                    high=c(1.8, 1.55))

forestplot(row_names,
       test_data$coef,
       test_data$low,
       test_data$high,
       zero = 1,
       cex  = 2,
       lineheight = "auto",
       xlab = "Lab axis txt")

好的,这将绘制一个情节。现在假设我想将它捕获到一个对象中,将它与另一个图并排绘制:

fp1 <- forestplot(row_names,
       test_data$coef,
       test_data$low,
       test_data$high,
       zero = 1,
       cex  = 2,
       lineheight = "auto",
       xlab = "Lab axis txt")

以下抛出错误:

> grid.arrange(fp1, fp1)
Hit <Return> to see next plot: 
Error in gList(list(path = "GRID.VP.7537", name = "arrange.1-1-1-1", n = 2L,  : 
only 'grobs' allowed in "gList"

cleary fp1 不是一个 grob - 但我如何通过其他方式实现它?

请参阅帮助页面中的第二个示例 ?forestplot。其中显示了如何执行此操作。


forestplot好像没有return剧情:看str(fp1).

几个选项是使用 grid 创建绘图 space (v1),或者捕获绘图然后合并 (v2)。


v1 使用网格

library(grid)
library(forestplot)

# Create 2 rows by one columns viewport
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 1)))

# Plot in viewport position 1x1
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
forestplot(row_names,
       test_data$coef,
       test_data$low,
       test_data$high,
       zero = 1,
       cex  = 2,
       lineheight = "auto",
       xlab = "Lab axis txt")
upViewport(1)

# Plot in viewport position 2x1
pushViewport(viewport(layout.pos.row=2, layout.pos.col=1))
forestplot(row_names,
       test_data$coef,
       test_data$low,
       test_data$high,
       zero = 1,
       cex  = 2,
       lineheight = "auto",
       xlab = "Lab axis txt", 
       new_page = FALSE)
upViewport(1)

v2,夺取剧情

fp1 <- grid.grabExpr(print(forestplot(row_names,
       test_data$coef,
       test_data$low,
       test_data$high,
       zero = 1,
       cex  = 2,
       lineheight = "auto",
       xlab = "Lab axis txt")))

gridExtra::grid.arrange(fp1, fp1)