1

Is there a way to conditional format a column of a tableGrob or gtable object and color (Red, yellow or Green) it based on a conditional format? For instance:

library(gridExtra)
library(grid)
d = head(iris, 20)
d
grob=tableGrob(d)

I wish to change Sepal.Width and color it based on the values of Petal.Length. If Petal.Length = 1.1 then set it to red, if it is 1.4 then set it to yellow and if it is 1.7 set it to green.

pnuts
  • 58,317
  • 11
  • 87
  • 139
KRStam
  • 393
  • 5
  • 18
  • have you tried to change the answer on yout previous question - they are *very similar*: https://stackoverflow.com/questions/50058750/r-tablegrob-heatmap-or-conditional-formating-in-column/50059929#50059929 – user20650 May 11 '18 at 12:30
  • Can't figure it out how to change colors based on certain criteria met on this occasion. – KRStam May 11 '18 at 12:34

1 Answers1

2

The condformat package can be used for that and its syntax is quite self-explanatory, I believe:

library(condformat)
data(iris)
condformat(head(iris, 20)) %>%
  rule_fill_discrete(
    columns = Sepal.Width,
    expression = Petal.Length,
    colours = c("1.1" = "red", "1.4" = "yellow", "1.7" = "green")) %>%
  condformat2grob()

result

zeehio
  • 4,023
  • 2
  • 34
  • 48