【问题标题】:ggplot Complex GridExtra Layout [duplicate]ggplot复杂的GridExtra布局[重复]
【发布时间】:2018-01-24 21:19:08
【问题描述】:

我想按以下方式布置三个地块:

[FirstPlot]
[2nd] [3rd]

第一个地块位于第二个和第三个地块之上。第一个图是第二个和第三个图宽度的两倍。现在让我们生成虚拟数据:

library(gridExtra)
library(grid)
library(ggplot2)
library(lattice)

plot1 <- qplot(1, 1)
plot2 <- qplot(1, 1)
plot3 <- qplot(1, 1)

lay <- rbind(c(1,1),
             c(2,3))

绘制出来的语法应该是:

grid.arrange(grobs = c(plot1, plot2, plot3), 
             layout_matrix = lay)

其实不应该这样,因为它不起作用。我收到以下错误。命令应该是什么来绘制这一切?我想我弄错了grobs 部分。

t:b 中的错误:NA/NaN 参数

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:

    grobs 参数需要 list() 而不是 c() :

    grid.arrange(grobs = list(plot1, plot2, plot3), 
                 layout_matrix = lay)
    

    ?grid.arrange
    

    grobs grobs 列表

    【讨论】:

    • 红利top = "Top Label" 给你一个头衔
    【解决方案2】:

    我想提供一个patchwork 解决方案,因为它是一个很酷的包:

    library(patchwork)
    library(ggplot2)
    
    plot1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    plot2 <- ggplot(diamonds, aes(cut, price)) + geom_boxplot()
    plot3 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
    
    # One way of doing it
    plot1 + (plot2 + plot3) + plot_layout(ncol = 1)
    
    # Another, alternative solution
    plot1 /
      (plot2 | plot3)
    

    返回:

    【讨论】:

      【解决方案3】:

      有一个简单的解决方法。使用grobs = list(plot1, plot2, plot3)

      【讨论】:

      • 我正在尝试,但收到此错误Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic --EDIT-- 我打错了。没关系,它有效!谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多