9

I have a table, and I want to print a title above it:

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

A similar question was asked here: Adding text to a grid.table plot

I've tried something similar and it doesn't work:

> 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
Community
  • 1
  • 1
Nate Reed
  • 6,761
  • 12
  • 53
  • 67

2 Answers2

14

Not sure what the problem was, but here is a working example:

enter image description here

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)
David C.
  • 1,974
  • 2
  • 19
  • 29
baptiste
  • 75,767
  • 19
  • 198
  • 294
4

Another option is:

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

You still might want to do some tweaking with the padding.

enter image description here

Michael Schubert
  • 2,726
  • 4
  • 27
  • 49