自从我更新到 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)
正如您将看到的,在顶部图表中,第一个面被扩展,而其他两个面在右侧被压缩。底部图表未覆盖所有宽度。
新的 ggplot2 版本是否会扰乱 gtable 宽度?
有人知道解决方法吗?
非常感谢
编辑:添加图表图片
我正在寻找类似的东西:
最佳答案
一种选择是将每个绘图整理到 3x3 gtable 中,其中中央单元格包裹所有绘图面板。
使用@SandyMuspratt 的示例
# devtools::install_github("baptiste/egg")
grid.draw(egg::ggarrange(plots=plots, ncol=1))
优点是,一旦采用这种标准化格式,无论面板、图例、轴、 strip 等的数量如何,都可以更轻松地以各种布局组合图。
grid.newpage()
grid.draw(ggarrange(plots=list(p1, p4, p2, p3), widths = c(2,1), debug=TRUE))
关于r - 使用多面 ggplot 2.0.0 和 gridExtra 安排公共(public)绘图宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797443/