2016-12-16 61 views
1

我有几个使用R程序包forestplot制作的森林地块。我想grid.arrange他们 - 但它似乎并不容易。Grid.arrange forestplot objects

例子:

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" 

所以克利FP1不是GROB - 但怎么做我通过其他方式实现了这一点?

回答

1

请参阅帮助页面?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) 
+0

THX用户。选项v1给我一个错误:'使用方法错误(“深度”): 没有适用于'深度'类应用于类“NULL”'的对象的方法。选项v2的作品,但在我真正的情节,我有标签和标题重叠,当我设置'gridExtra :: grid.arrange(fp1,fp2。ncols = 2)' – user2498193

+1

@ user2498193;当您将v1答案复制并粘贴到新的R会话时,是否会收到错误? (因为我不能重现)。 Re v2;它更具有气质,因为您需要调整输出窗口或设备的尺寸来确定合适的位置。 – user20650

+0

在新的会话中仍然会出现错误。 Eh re v2 - 不知道该怎么做。我开始认为我可能只是手动执行此操作 – user2498193