Remove white space between plots and table in grid

2020-02-29 00:23发布

I would like to remove the large spacing that is inserted by default between the plots and the table in a grid.arrange, as shown in the MWE hereafter:

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)

I suspect this behaviour is due to the tableGrob, but I couldn't find any answer treating this issue.

Thanks in advance!

标签: r gridextra grob
2条回答
小情绪 Triste *
2楼-- · 2020-02-29 00:50

I actually found the parameter ruling the spacing between grobs: heights, see line below

grid.arrange(plots, table, heights=c(5,1))
查看更多
ら.Afraid
3楼-- · 2020-02-29 00:56

grid.arrange() by default allocates equal space for each cell. If you want a tight fit around a specific grob, you should query its size, and pass it explicitly,

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

查看更多
登录 后发表回答