10

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!

JohnBee
  • 1,720
  • 1
  • 15
  • 19
  • Would this help? http://stackoverflow.com/questions/13299496/reduce-space-between-grid-arrange-plots – Nemesi Aug 21 '15 at 14:22
  • Not exactly, it behaves differently between two ggplots that between a plot and a tableGrob. But thanks! – JohnBee Aug 21 '15 at 14:26
  • Possible duplicate of [Reducing space between two plots in grid.arrange](http://stackoverflow.com/questions/26146735/reducing-space-between-two-plots-in-grid-arrange) – kdarras Aug 22 '16 at 09:45

2 Answers2

8

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

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • 1
    this answer fails me with: `Error in arrangeGrob(...) : could not find function "unit.c"` – ecoe Jul 07 '16 at 00:09
4

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

grid.arrange(plots, table, heights=c(5,1))
JohnBee
  • 1,720
  • 1
  • 15
  • 19