1

Trying to use the grid.table() function to display a table. If a value is less than 0, then display as red, otherwise, display as green if it's greater than 0. I'm having trouble locating examples of usage; I came across this page and it seems to hard-code any individual cell formatting.

So is there a way to color-code the cells without having to hard-code the exact cell reference number?

Here's some data:

structure(list(data_2018 = c(4, 3, 4, NaN, NaN, NaN), 
data_2017 = c(3, 
1, 1, NaN, NaN, NaN), 
pilot = c(1.31578947368421, -0.0526315789473699, 
1.21052631578947, NaN, NaN, NaN), 
all = c(1.22222222222222, 
-0.18518518518519, 1.07407407407407, NaN, NaN, NaN), 
general = c(1.20833333333333, 
-0.0833333333333299, 1.16666666666667, NaN, NaN, NaN)), 
class = "data.frame", row.names = c(NA, 
-6L))

I've used the formattable package which works, but I need the output to be compatible with ggplot so I'm back at square one using grid.table()

zx8754
  • 52,746
  • 12
  • 114
  • 209
simplycoding
  • 2,770
  • 9
  • 46
  • 91
  • 1
    See this answer from linked post: https://stackoverflow.com/a/39324604/680068 , we need to supply same size matrix for colors based on our dataframe. – zx8754 Jan 29 '19 at 19:25
  • Got it. Is there a way to create a matrix with the logic I mentioned earlier? Or would I have to instantiate the matrix like in the answer, then iterate through all the cells to update them to update as either "red" or "green"? – simplycoding Jan 29 '19 at 19:31
  • 1
    Something like `mycols <- ifelse(mydata < 0, "red", "green")`. – zx8754 Jan 29 '19 at 19:33

0 Answers0