Search code examples
rgt

How to set fixed colors for the different levels of a categorical variable in gt()'s data_color() function?


I would like to set fixed colors for the levels of a categorical variable used in a gt table. Take a look at the example below. I want the level "a" always to be colored in red, "b" always to be orange, and "c" always to be colored in green, no matter if some of the levels are missing or not.

library(tidyverse)
library(gt)

data <- tibble(
  var1 = c(1, 2, 3, 4, 5),
  var2 = c("a", "b", "b", "c", "c"),
  var3 = c("a", "a", "c", "c", "c"))

  data %>% 
  gt() %>% 
  data_color(columns = c(var2, var3), method = "factor", 
             palette = c("red", "orange", "darkgreen"))

Solution

  • One option which worked for me was to convert the columns to factors with the levels set to include all possible options. Would have expected that the levels= or domain= arguments can be used to achieve that but wasn't able to figure out how to apply them. Perhaps I miss something obvious.

    library(tidyverse)
    library(gt)
    
    data %>%
      mutate(
        across(
          c(var2, var3),
          ~ factor(.x, levels = c("a", "b", "c"))
        )
      ) %>%
      gt() %>%
      data_color(
        columns = c(var2, var3), method = "factor",
        palette = c("red", "orange", "darkgreen")
      )
    

    enter image description here