Jor*_*dal 6 r ggplot2 gridextra gtable
由于我已经更新到ggplot2 2.0.0,我无法使用gridExtra正确排列图表.问题是分面图表会被压缩,而其他图表会扩展.宽度基本搞砸了.我想安排它们类似于这些单面图的方式:左对齐两个图形边(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)
Run Code Online (Sandbox Code Playgroud)
正如您将看到的,顶部图表,第一个方面被扩展,而另外两个方面被压缩.底部图表未涵盖所有宽度.
是不是新的ggplot2版本搞乱了gtable的宽度?
有人知道解决方法吗?
非常感谢你
编辑:添加图表的图片
我正在寻找类似的东西:
一种选择是将每个地块按摩成3x3 gtable,其中中央细胞包裹所有的情节面板.
使用@SandyMuspratt中的示例
# devtools::install_github("baptiste/egg")
grid.draw(egg::ggarrange(plots=plots, ncol=1))
Run Code Online (Sandbox Code Playgroud)
优点是,一旦采用这种标准化格式,无论面板,图例,轴,条带等的数量如何,绘图都可以更容易地组合成各种布局.
grid.newpage()
grid.draw(ggarrange(plots=list(p1, p4, p2, p3), widths = c(2,1), debug=TRUE))
Run Code Online (Sandbox Code Playgroud)
小智 0
去掉这两条线并保留其余的,效果很好。
g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable
Run Code Online (Sandbox Code Playgroud)
可能是它限制了它们的宽度。
| 归档时间: |
|
| 查看次数: |
3618 次 |
| 最近记录: |