How do you add grouped column headers to a gtable/tableGrob object?


So I am trying to create a figure with a plot on top, and some tabular data underneath. I have found this is generally easy to do using gridExtra::grid.arrange(), however, I have not been able to figure out how to add grouped column headers, if it is even possible.

Here is some code to build off of.


cors <- 
  iris %>% 
  select(-Species) %>% 

p <-
  cors %>%
  corrr::stretch() %>% 
  ggplot(aes(x, y, fill = r)) +

t <- tableGrob(cors, rows = NULL, cols = c('', 'Length', 'Width',
                                           'Length', 'Width'))

grid.arrange(p,t, heights = c(3, 1))

So I essentially want to be able to group columns 2 and 3 under a common header Sepal, and group columns 4 and 5 under the common header Petal. I know this sort of thing is doable using kabelExtra using the kableExtra::add_header_above() function. But I am much less familiar with manipulating gtable objects.

If someone has a better way to do this in R entirely, I am open to suggestions.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.