Align grob at fixed top/center position, regardless of size

前端 未结 1 1149
半阙折子戏
半阙折子戏 2020-12-10 21:36

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 sta

1条回答
  •  -上瘾入骨i
    2020-12-10 22:10

    using the code from the other answer, I get this

    library(gridExtra)
    justify <- function(x, hjust="center", vjust="center", draw=TRUE){
      w <- sum(x$widths)
      h <- sum(x$heights)
      xj <- switch(hjust,
                   center = 0.5,
                   left = 0.5*w,
                   right=unit(1,"npc") - 0.5*w)
      yj <- switch(vjust,
                   center = 0.5,
                   bottom = 0.5*h,
                   top=unit(1,"npc") - 0.5*h)
      x$vp <- viewport(x=xj, y=yj)
      if(draw) grid.draw(x)
      return(x)
    }
    
    library(gridExtra)
    
    tg <- list(tableGrob(iris[1:3, 1:2]), tableGrob(iris[1:5, 1:2]))
    tgt <- lapply(tg, justify, vjust="top", draw=FALSE)
    grid.newpage()
    grid.arrange(grobs=tgt, ncol=2)
    

    0 讨论(0)
提交回复
热议问题