r - 使用多面 ggplot 2.0.0 和 gridExtra 安排公共(public)绘图宽度

标签 r ggplot2 gridextra gtable

自从我更新到 ggplot2 2.0.0 后,我无法使用 gridExtra 正确排列图表。问题是多面图表将被压缩,而其他图表将扩展。宽度基本上是困惑的。我想以类似于这些单面图的方式排列它们:left align two graph edges (ggplot)

我放置了可重现的代码

library(grid) # for unit.pmax()
library(gridExtra)

plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(. ~ Species) + 
  stat_smooth(method = "lm")

plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths # extract the first three widths, 
mpg.widths <- g.mpg$widths # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths)

g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable

grid.arrange(g.iris,g.mpg,ncol=1)

enter image description here

正如您将看到的,在顶部图表中,第一个面被扩展,而其他两个面在右侧被压缩。底部图表未覆盖所有宽度。

新的 ggplot2 版本是否会扰乱 gtable 宽度?

有人知道解决方法吗?

非常感谢

编辑:添加图表图片

我正在寻找类似的东西:

enter image description here

最佳答案

一种选择是将每个绘图整理到 3x3 gtable 中,其中中央单元格包裹所有绘图面板。

使用@SandyMuspratt 的示例

# devtools::install_github("baptiste/egg") 
grid.draw(egg::ggarrange(plots=plots, ncol=1))

enter image description here

优点是,一旦采用这种标准化格式,无论面板、图例、轴、 strip 等的数量如何,都可以更轻松地以各种布局组合图。

grid.newpage()
grid.draw(ggarrange(plots=list(p1,  p4, p2, p3), widths = c(2,1), debug=TRUE))

enter image description here

关于r - 使用多面 ggplot 2.0.0 和 gridExtra 安排公共(public)绘图宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797443/

相关文章:

r - 将 geom_segment 与 coord_pol 一起使用

r - 如何在包含一组ggplot2散点图的gridExtra中通过arrange.grid创建的图表周围添加边框

r - 如何根据同一列中的值创建新变量?

r - 如何在R中的数据框中组合两列?

r - 设置 R 控制台的标题

r - 在 Shiny 的应用程序中显示 ggplot 时,如何捕获控制台中出现的 ggplot 警告并显示在应用程序中?

r - dplyr 合并多个结果

r - 识别相关图中落在 CI 之外的数据点

r - 如何防止 marrangeGrob 打开图形设备

r - 多页,每页有几个 ggplot2 图表和表格,来自多个数据框,每个页面都是一个共同因素的水平