4

How do I align all tables in grid.arrange to top? The tables seem to mid-align by default.

df1 = data.frame(x=c(1, 2, 3), y=c('a', 'b', 'c'))
df2 = data.frame(x=rep(1, 10), y=rep('a', 10))
grid.arrange(tableGrob(df1), tableGrob(df2),
             nrow=1, ncol=2)

enter image description here

shawnl
  • 1,861
  • 1
  • 15
  • 17

1 Answers1

5
g1 <- tableGrob(df1)    
g2 <- tableGrob(df2)

grid.draw(combine(g1, g2, along=1))

enter image description here

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • Thanks! I tried to mix in textGrob but it doesn't work. Could you point me to the right direction? title1 = textGrob('Title 1') title2 = textGrob('Title 2') grid.draw(title1, title2, combine(g1, g2, along=1)) – shawnl Jun 15 '17 at 08:20
  • Can I check if your intention were to add a title on top of each tableGrob object? If so, [this may help](https://stackoverflow.com/questions/31640916/how-can-i-add-a-title-to-a-tablegrob-plot) – Adam Quek Jun 15 '17 at 08:27
  • Also, you could check out [this](https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html), which is quite helpful for some other layout. – Adam Quek Jun 15 '17 at 08:30