0

For a dummy data as follows:

df <- structure(list(id = 1:4, v1 = c("/", "0.2", "0.3", "0.4"), v2 = c(0.8, 
0.2, 0.1, 0.5), change = c("/", "0", "-0.2", "0.1")), class = "data.frame", row.names = c(NA, 
-4L))

With code below, I can easily coloring v2 with gt package from R since its data type is numeric:

library(tidyverse)
library(gt)

df %>%
  gt() %>%
  data_color(
    columns = "v2",
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
        ) %>% as.character(),
      domain = NULL
      )
  )

Out:

enter image description here

Now I would like to color v1 as well (pls note this column's dtype is char) and display NA with / (since the output figure will be used in report, so I wish to \ or - as symbol to represent NA or NaN rather than using R's default NA symbol).

So my question is it's possible to do so? If yes, how? Thanks for your help at advance.

enter image description here

Edit:

library(tidyverse)
library(gt)
library(paletteer)

na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material") 
na_palette[1] <- "#FFFFFFFF" # replace  "/" with white

df %>%
  gt() %>%
  data_color(
    columns = "v2",
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
      ) %>% as.character(),
      domain = NULL
    )
  ) %>%
  data_color(
    columns = "v1",
    colors = scales::col_factor(
      na.color = "white",
      palette = na_palette %>% as.character(),#use the palette
      domain = NULL
    )
  )%>%
  data_color(
    columns = "change",
    colors = scales::col_factor(
      na.color = "white",
      palette = na_palette %>% as.character(),#use the palette
      domain = NULL
    )
  )

Out:

enter image description here

ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

1

you can add another gt::data_color call to your pipe:

df %>%
  gt() %>%
  data_color(
    columns = "v2",
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
      ) %>% as.character(),
      domain = NULL
    )
  )%>%
  data_color(
    columns = "v1",
    colors = scales::col_factor(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
      ) %>% as.character(),
      domain = NULL
    )
  )

in that case you just need to change the scales::col_numeric to scales::col_factor which sets colors for your character values

edit: if you want to set the / to white:

na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material") # get the palette

na_palette[1] <- "#FFFFFFFF" # replace  "/" with white


df %>%
  dplyr::mutate(
    change = factor(change, levels = change),
    v1 = factor(v1, levels = v1)
  ) %>% 
  gt() %>%
  data_color(
    columns = "v2",
    colors = scales::col_numeric(
      palette = paletteer::paletteer_d(
        palette = "ggsci::red_material"
      ) %>% as.character(),
      domain = NULL
    )
  ) %>%
  data_color(
    columns = c("v1", "change"),
    colors = scales::col_factor(
      palette = na_palette %>% as.character(),#use the palette
      domain = NULL
    )
  )

Out:

enter image description here

ah bon
  • 9,293
  • 12
  • 65
  • 148
Domingo
  • 613
  • 1
  • 5
  • 15
  • Sorry, the color represent values, if it's larger or smaller, so I updated code, pls test with updated `palette`. – ah bon Oct 13 '21 at 09:25
  • should still work with `scales::col_factor` for v1, I updated the answer – Domingo Oct 13 '21 at 09:28
  • Sorry, my mistake. BTW, is it possible set no color for cell `/` since it represent `NA`s? – ah bon Oct 13 '21 at 10:06
  • 1
    i adapted my answer, you might also think of `n = NROW(df)` in `paletteer::paletteer_d`, so every value also gets a color – Domingo Oct 14 '21 at 05:52
  • I apply your code to `change` column, but a value use `na_palette` instead of `/`. – ah bon Oct 14 '21 at 07:52
  • 1
    I changed my answer: if you change the data type of the columns to factor with the correct levels it works, the problem was before that the `col_factor` set the wrong levels – Domingo Oct 14 '21 at 09:17
  • Problem perfertly solved, many thanks. – ah bon Oct 14 '21 at 09:42
  • Seems we can use alternative solution as well: https://stackoverflow.com/questions/68911736/special-zero-value-in-gt-tables – ah bon Oct 14 '21 at 09:51