I have some table grobs, some long and some short. I'd like to draw these at the top/center of the page (with a small margin).
From this answer, I got a helpful starting point, but the positioning is dependent on each grob's height.
library(gridExtra)
# fake data
my_df <- data.frame(col1=rep('hello', 35), col2=round(rnorm(35), 3))
# list of grobs
tg <- list(tableGrob(my_df[1:30, ]), tableGrob(my_df[31:35, ]))
# this positions grobs at center top, but varies based on rows in table
tg[[1]]$vp <- viewport(x = 0.5,
y = unit(1,"npc") - 0.52 * grobHeight(tg[[1]]))
tg[[2]]$vp <- viewport(x = 0.5,
y = unit(1,"npc") - 0.52 * grobHeight(tg[[2]]))
# this one appears just below top, which is good
grid.newpage()
grid.draw(tg[[1]])
# this appears slightly closer to the top; desired outcome is to have column headers
# in same position as the previous one
grid.newpage()
grid.draw(tg[[2]])
I've been experimenting with different parameters to the viewport
call -- e.g.
viewport(x = 0.5, y = unit(0.95, 'npc'), just=c('center', 'top'))
but haven't been successful. Any help appreciated!