【问题标题】:How can I add a title to a tableGrob plot?如何向 tableGrob 图添加标题?
【发布时间】:2018-01-18 19:09:41
【问题描述】:

我有一张桌子,我想在上面打印一个标题:

t1 <- tableGrob(top_10_events_by_casualties, cols=c("EVTYPE", "casualties"), rows=seq(1,10))
grid.draw(t1)

这里提出了类似的问题:Adding text to a grid.table plot

我尝试过类似的方法,但它不起作用:

> title <- textGrob("Title",gp=gpar(fontsize=50))
> table <- gtable_add_rows(t1, 
+                          heights = grobHeight(title) + padding,
+                          pos = 0)
Error: is.gtable(x) is not TRUE

【问题讨论】:

    标签: r gridextra


    【解决方案1】:

    不确定问题出在哪里,但这里有一个工作示例:

    library(grid)
    library(gridExtra)
    library(gtable)
    
    t1 <- tableGrob(head(iris))
    title <- textGrob("Title",gp=gpar(fontsize=50))
    padding <- unit(5,"mm")
    
    table <- gtable_add_rows(
         t1, 
         heights = grobHeight(title) + padding,
         pos = 0)
    table <- gtable_add_grob(
        table, 
        title, 
        1, 1, 1, ncol(table))
    
    grid.newpage()
    grid.draw(table)
    

    【讨论】:

    • 太棒了!我认为您只能指定一次“标题”。
    • 这是一个不错的解决方案,但如果标题比表格宽就会失败。
    • @mrbrich 在这种情况下使用clip = 'off'
    【解决方案2】:

    另一种选择是:

    library(gridExtra)
    grid.arrange(top="Iris dataset", tableGrob(head(iris)))
    

    您可能仍想对填充进行一些调整。

    【讨论】:

    • 我喜欢这个解决方案,但是如何最好地调整填充?
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    相关资源
    最近更新 更多