【问题标题】:Grid.arrange forestplot objectsGrid.arrange 森林图对象
【发布时间】:2016-12-17 03:03:26
【问题描述】:

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

【问题讨论】:

    标签: r r-grid forestplot


    【解决方案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)
    

    【讨论】:

    • 感谢用户。选项 v1 给了我一个错误:Error in UseMethod("depth") : no applicable method for 'depth' applied to an object of class "NULL" 。选项 v2 有效,但在我的真实情节中,当我设置 gridExtra::grid.arrange(fp1, fp2. ncols=2) 时,我的标签和标题重叠
    • @user2498193 ;当您将 v1 答案复制并粘贴到新的 R 会话中时,您会收到错误吗? (因为我无法重现)。修订版 2;它有点喜怒无常,因为您需要调整输出窗口或设备的大小以适合定位。
    • 是的,在新的会话中仍然会出现错误。 Eh re v2 - 不知道如何做到这一点。我开始认为我可能只是手动执行此操作
    • Re v1:这似乎是本地 PC 问题。你得到的错误的快速internet search 似乎与 ggplot / grid plots 类似:也许那里有一些东西可以给你一个提示。 Re v2 我的意思是调整输出设备的大小,例如,pdf("test.pdf", width=10, hieght=10) ; gridExtra::grid.arrange(fp1, fp1) ; dev.off(),您可以在其中更改宽度和高度以适应。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2017-12-04
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多