在pdf顶部排列tableGrob

在pdf顶部排列tableGrob,r,pdf,gridextra,R,Pdf,Gridextra,我想将tableGrob图表与pdf顶部对齐。默认设置为中间位置。我的问题是当我有不同行号的图表时 library(ggplot2) g <- gridExtra::tableGrob(head(iris)) g2 <- gridExtra::tableGrob(iris[1:3,]) pdf("test.pdf") gridExtra::grid.arrange(g, heights = c(1,3), padding = unit(1,"in"), top = "Title

我想将tableGrob图表与pdf顶部对齐。默认设置为中间位置。我的问题是当我有不同行号的图表时

library(ggplot2)

g <- gridExtra::tableGrob(head(iris))
g2 <- gridExtra::tableGrob(iris[1:3,])

pdf("test.pdf")
gridExtra::grid.arrange(g, heights = c(1,3), padding = unit(1,"in"), top = 
"Title")
gridExtra::grid.arrange(g2, heights = c(1,3), padding = unit(1,"in"), top = 
"Title2")

dev.off()
库(ggplot2)

关于同一个话题,有很多问题,其他问题的答案都是这样的

如果你遇到同样的错误,也许有人可以对此发表评论

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)
5.stop("both operands must be units")
4.Ops.unit(unit(1, "npc"), 0.5 * h)
3.FUN(X[[i]], ...)
2.lapply(myplot, justify, vjust = "top", draw = FALSE)