在grid.arrange中去除图形和表格之间的空白

10

我希望能够在grid.arrange中移除默认插入的图表和表格之间的大间距,如下所示:

require(ggplot2)
require(gridExtra)

list1=data.frame(mtcars[1:3, ])  # Dummy data
p1 = ggplot(list1, aes(mpg,cyl)) + geom_point()  # Dummy plot
p2 = ggplot(list1, aes(disp,hp)) + geom_point()  # Dummy plot
plots <- arrangeGrob(p1, p2,nrow=2)

table <- tableGrob(list1)
grid.arrange(plots, table)

我怀疑这种行为是由于tableGrob引起的,但我找不到任何解决此问题的答案。

提前感谢!


这个能帮到你吗?https://dev59.com/QmYr5IYBdhLWcg3w6eM3 - Nemesi
不完全一样,它在两个ggplot之间的行为与在plot和tableGrob之间的行为不同。但还是谢谢! - JohnBee
可能是Reducing space between two plots in grid.arrange的重复问题。 - kdarras
2个回答

8

grid.arrange() 默认为每个单元格分配相同的空间。如果您想紧密围绕特定的grob安排,您应该查询其大小,并明确传递它。

grid.arrange() 默认情况下为每个单元格分配相同的空间。如果您想让grob占据更小的空间,请查询其大小并明确传递它。

library(grid)
th <- sum(table$heights) # note: grobHeights.gtable is inaccurate
grid.arrange(plots, table, heights = unit.c(unit(1, "null"), th))

enter image description here


1
这个答案给我返回了错误信息:Error in arrangeGrob(...) : could not find function "unit.c" - ecoe

4
我实际上找到了控制grobs高度之间距离的参数:heights,如下所示。
grid.arrange(plots, table, heights=c(5,1))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接