从 https://stackoverflow.com/a/13295880 我学会了如何用对齐的绘图区域排列两个绘图。
我的问题是:我怎样才能得到一个排列好的图的对象?
例子:
require(ggplot2)
require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip()
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
## works:
grid.arrange(gA, gB, ncol=1)
## does not work:
theplot <- grid.arrange(gA, gB, ncol=1, plot=FALSE)
最佳答案
使用函数 arrangeGrob()
将两个图都保存为对象。
theplot <- arrangeGrob(gA, gB, ncol=1)
关于r - 使用 gtable 对象进行排列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15942313/