Search code examples
rgridextragrob

how to show the title of a table with tableGrob?


Consider this example

library(gridExtra)
library(grid)

d1 <- head(iris[,1:3]) %>% as_tibble()
d2 <- head(iris[,2:5]) %>% as_tibble()

grid.arrange(tableGrob(d1),
             tableGrob(d2))

which gives enter image description here

This is really nice but I am missing the titles of the dataframes! How can I add them to the picture automatically?

Thanks!


Solution

  • You can use ggplot,

    library(ggplot2)
    
    dd1 <- ggplot() + annotation_custom(tableGrob(d1)) + labs(title = 'd1')
    dd2 <- ggplot() + annotation_custom(tableGrob(d2)) + labs(title = 'd2')
    grid.arrange(dd1, dd2, nrow = 2)
    

    enter image description here