问题 减少grid.arrange图之间的空间


我问了一个问题 这里 关于电网安排并得到了极好的回应。我想现在缩小绘图之间的空间但是会出错。首先,我提出有效的代码,然后是错误代码(我尝试过)。我实际上找不到 grid.arrange 并且一直认为它来自 gridExtra 但我可能不对。

所以2部分:

  1. 如何通过网格排列减少绘图之间的空间
  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)

3520
2017-11-08 23:07


起源



答案:


我误解了ggplot:

require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
    coord_flip() + ylab("") + theme(plot.margin= unit(c(1, 1, -1, 1), "lines"))
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() + 
    theme(plot.margin= unit(rep(.5, 4), "lines"))


 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)

12
2017-11-08 23:20





是, 该 DOC 说: plot.margin |整个图周围的边距(具有顶部,右侧,底部和左侧边距大小的单位)


-2
2018-05-05 09:02