2016-12-16 62 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