有一种方法,但不清楚当文本太宽而无法放入某些单元格时会发生什么。
一种选择是手动设置宽度,
library(grid)
library(gridExtra)
g1 <- g2 <- tableGrob(head(iris, 10), rows=NULL)
g2$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc")
grid.newpage()
gt = arrangeGrob(textGrob("page 1"), textGrob("page 2"),
rectGrob(gp=gpar(fill="grey98")),
rectGrob(gp=gpar(fill="grey98")),
nullGrob(),
layout_matrix=rbind(c(1,5,2), c(3,5,4)),
widths = unit(c(1,5,1),c("null", "cm", "null")),
heights = unit(c(1, 1),c("line", "null")),
vp = viewport(width=0.9, height=0.9))
tc = list(g1, g2)
gt <- gtable::gtable_add_grob(gt, tc, l=c(1,3), t=2,
name="newgrobs")
grid.draw(gt)
当然,对于固定的字体大小,这意味着可能会剪切一些文本。
可能更好的选择是引入换行符,和/或(稍微)减小字体大小。
g3 <- tableGrob(head(iris, 10), theme = ttheme_default(7),
rows=NULL, cols=gsub("\\.", "\\\n",names(iris)))
g3$widths <- unit(rep(1/ncol(g2), ncol(g2)), "npc")
grid.newpage()
gt = arrangeGrob(textGrob("page 1"), textGrob("page 2"),
rectGrob(gp=gpar(fill="grey98")),
rectGrob(gp=gpar(fill="grey98")),
nullGrob(),
layout_matrix=rbind(c(1,5,2), c(3,5,4)),
widths = unit(c(1,1,1),c("null", "line", "null")),
heights = unit(c(1, 1),c("line", "null")),
vp = viewport(width=0.9, height=0.9))
tc = list(g2, g3)
gt <- gtable::gtable_add_grob(gt, tc, l=c(1,3), t=2,
name="newgrobs")
grid.draw(gt)