1

我有几个使用 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 - 但我如何通过其他方式实现这一点?

4

1 回答 1

2

请参阅帮助页面中的第二个示例?forestplot。这显示了如何做到这一点。


forestplot似乎没有返回情节:看看str(fp1).

有几个选项grid用于创建绘图空间 (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)
于 2016-12-16T18:56:45.030 回答