Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R gridExtra: modify theme on the fly for a single tableGrob?

I read Baptiste's excellent explanation of gridextra's tableGrob functions here: https://github.com/baptiste/gridextra/wiki/tableGrob

From his section on Aesthetic formatting:

tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
  core=list(bg_params = list(fill = blues9[1:4], col=NA),
            fg_params=list(fontface=3)),
  colhead=list(fg_params=list(col="navyblue", fontface=4L)),
  rowhead=list(fg_params=list(col="orange", fontface=3L)))

grid.arrange(
  tableGrob(iris[1:4, 1:2], theme=tt1),
  tableGrob(iris[1:4, 1:2], theme=tt2),
  tableGrob(iris[1:4, 1:2], theme=tt3),
  nrow=1)

I wonder is it possible to modify a theme "on-the-fly" for a particular tableGrob, e.g., something like:

grid.arrange(
  tableGrob(iris[1:4, 1:2], theme=tt1 + theme_default(core=list(fg_params=list(cex=0.7))),
  tableGrob(iris[1:4, 1:2], theme=tt2),
  tableGrob(iris[1:4, 1:2], theme=tt3),
  nrow=1)

This last block of code does NOT work, but what I wanted to do was modify theme "tt1" on the fly to change the core text size JUST for the first tableGrob, WITHOUT permanently changing the theme "tt1".

Thanks!

like image 775
TrialNError Avatar asked Sep 03 '25 07:09

TrialNError


1 Answers

Themes appear to be just lists. You can update properties of lists with modifyList. For example

grid.arrange(
  tableGrob(iris[1:4, 1:2], theme=modifyList(tt1, list(core=list(fg_params=list(cex=0.7))))),
  tableGrob(iris[1:4, 1:2], theme=tt2),
  tableGrob(iris[1:4, 1:2], theme=tt3),
  nrow=1)
like image 64
MrFlick Avatar answered Sep 04 '25 21:09

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!