我问了一个问题 这里 关于电网安排并得到了极好的回应。我想现在缩小绘图之间的空间但是会出错。首先,我提出有效的代码,然后是错误代码(我尝试过)。我实际上找不到 grid.arrange
并且一直认为它来自 gridExtra
但我可能不对。
所以2部分:
- 如何通过网格排列减少绘图之间的空间
- 我在哪里可以找到有关的文档
grid.arrange
(Baptiste我知道你维护gridExtra所以如果我没有按照预期的方式使用它,请更正我的想法或使用包。)
好代码坏空间
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("")
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)
grid.arrange(gA, gB, ncol=1)
糟糕的代码(我的尝试)
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("") + theme(plot.margin= unit(1, "cm"))
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)
grid.arrange(gA, gB, ncol=1)
错误:
Error in `[.unit`(theme$plot.margin, 2) :
Index out of bounds (unit subsetting)