Search code examples
rgt

How to color columns' numeric cells while ignoring character vectors represents NA in gt package from R


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


Solution

  • 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