How do I fit a very wide grid.table or tableGrob to fit on a pdf page?

前端 未结 3 703
慢半拍i
慢半拍i 2020-12-02 02:13

I have a fairly wide table (4/3 of page width) that I\'m trying to print using grid.table or grid.arrange (via tableGrob) into a pdf file. The table goes beyond page bounda

相关标签:
3条回答
  • 2020-12-02 02:25

    I got this done using font sizes. Not the best solution (requires manual intervention) but maybe someone can contribute something more elegant.

    termTable = tableGrob(terms, h.even.alpha=1, h.odd.alpha=1,  v.even.alpha=0.5, v.odd.alpha=1, core.just='left', rows=c(),
    gpar.coretext =gpar(fontsize=8),
    gpar.coltext=gpar(fontsize=10, fontface='bold'),
    gpar.rowtext=gpar(fontsize=10, fontface='bold')
    )
    
    0 讨论(0)
  • 2020-12-02 02:44

    There is a way, but it's unclear what should happen when the text is too wide to fit in some cells.

    One option is to set the widths manually,

    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)
    

    but of course with a fixed font size it means that some text might be cut.

    Probably a better option is to introduce line breaks, and/or (slightly) reduce the font size.

    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)
    

    0 讨论(0)
  • 2020-12-02 02:44

    With the most recent version of gridExtra, the correct formatting to update rimorob's answer is:

    termTable = tableGrob(terms, theme =ttheme_default(gpar.coretext =gpar(fontsize=8), gpar.coltext=gpar(fontsize=10, fontface='bold'), gpar.rowtext=gpar(fontsize=10, fontface='bold') ))

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