用分面ggplot 2.0.0& amp;& gridExtra [英] Arrange common plot width with facetted ggplot 2.0.0 & gridExtra
问题描述
由于我已更新到ggplot2 2.0.0,因此无法使用gridExtra正确安排图表。问题在于分面图表会被压缩,而其他图表会被展开。宽度基本上是混乱的。我想把它们安排成类似于这些单面图的方式:
正如你所看到的,最上面的图表,第一个面被扩展,而另外两个被压缩在右边。底图不包括所有宽度。
难道新的ggplot2版本与gtable宽度有关吗?
编辑:添加了图表的图片
p>我在找类似的东西:
p>
其中一个选项是将每个绘图按摩到一个3x3 gtable,其中中央单元格包裹所有绘图面板。
使用@SandyMuspratt中的示例
#devtools :: install_github (baptiste / egg)
grid.draw(egg :: ggarrange(plots = plots,ncol = 1))
Since I have updated to ggplot2 2.0.0, I cannot arrange charts propperly using gridExtra. The issue is that the faceted charts will get compressed while other will expand. The widths are basically messed up. I want to arrange them similar to the way these single facet plots are: left align two graph edges (ggplot)
I put a reproducible code
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)
As you will see, the top chart, the first facet is expanded while the other 2 get compressed at the right. Bottom chart does not cover all width.
Could it be that the new ggplot2 version is messing with the gtable widths?
Anyone know a workaround?
Thank you very much
EDIT: Added picture of chart
I'm looking for something like:
one option is to massage each plot into a 3x3 gtable, where the central cell wraps all the plot panels.
Using the example from @SandyMuspratt
# devtools::install_github("baptiste/egg")
grid.draw(egg::ggarrange(plots=plots, ncol=1))
the advantage being that once in this standardised format, plots may be combined in various layouts much more easily, regardless of number of panels, legends, axes, strips, etc.
grid.newpage()
grid.draw(ggarrange(plots=list(p1, p4, p2, p3), widths = c(2,1), debug=TRUE))
这篇关于用分面ggplot 2.0.0& amp;& gridExtra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!