Using palettes with gt

This vignette shows you how to use palettes to colour data cells with gt.

library(palettes)
library(gt)
library(scales)
library(dplyr)

Preparing the table

To demonstrate how to use palettes to colour data cells with gt, we will use airquality as an input data table with the following columns:

Temperature is a good fit for colour—we can represent cold with blue and hot with red.

First we create the gt table we will add colour to. The table is slightly modified from a table in the Introduction to Creating gt Tables article. It looks like this.

# Modify the `airquality` dataset by adding the year
# of the measurements (1973) and limiting to 10 rows
airquality_m <- 
  airquality %>%
  mutate(Year = 1973L) %>%
  slice(1:10) %>% 
  select(Year, Month, Day, Temp)
  
# Create a display table using the `airquality`
# dataset; arrange columns into groups
gt_tbl <- 
  gt(airquality_m) %>%
  tab_header(
    title = "New York Temperature Measurements",
    subtitle = "Daily measurements in New York City (May 1-10, 1973)"
  ) %>%
  tab_spanner(
    label = "Time",
    columns = c(Year, Month, Day)
  ) %>%
  tab_spanner(
    label = "Measurement",
    columns = c(Temp)
  ) %>%
  cols_label(
    Temp = html("Temp,<br>&deg;F")
  )

# Show the gt table
gt_tbl
New York Temperature Measurements
Daily measurements in New York City (May 1-10, 1973)
Time Measurement
Year Month Day Temp,
°F
1973 5 1 67
1973 5 2 72
1973 5 3 74
1973 5 4 62
1973 5 5 56
1973 5 6 66
1973 5 7 65
1973 5 8 59
1973 5 9 61
1973 5 10 69

Adding colour

We can use the Hiroshige palette for blue and red gradients, reversing it so the colours are in the correct order for this example.

colour_vector <- rev(met_palettes$Hiroshige)
colour_vector
#> <palettes_colour[10]>
#> • #1E466E
#> • #376795
#> • #528FAD
#> • #72BCD5
#> • #AADCE0
#> • #FFE6B7
#> • #FFD06F
#> • #F7AA58
#> • #EF8A47
#> • #E76254

At the moment, objects of class palettes_palette or palettes_colour cannot be used directly with the colour functions in gt. To work around this we cast the colour vector to a character vector.

character_vector <- as.character(colour_vector)
character_vector
#>  [1] "#1e466e" "#376795" "#528fad" "#72bcd5" "#aadce0" "#ffe6b7" "#ffd06f"
#>  [8] "#f7aa58" "#ef8a47" "#e76254"

Now we can use gt::data_color() to colour the temperature cells. The colors argument accepts either a vector of colours to use for each distinct cell value or level or a colour mapping function (e.g., from the scales package).

Here we pass the character vector directly to the colors argument.

gt_tbl %>% 
  data_color(
    columns = Temp,
    palette = character_vector
  )
New York Temperature Measurements
Daily measurements in New York City (May 1-10, 1973)
Time Measurement
Year Month Day Temp,
°F
1973 5 1 67
1973 5 2 72
1973 5 3 74
1973 5 4 62
1973 5 5 56
1973 5 6 66
1973 5 7 65
1973 5 8 59
1973 5 9 61
1973 5 10 69

But this works equally well with the colour mapping functions from palettes.

gt_tbl %>% 
  data_color(
    columns = Temp,
    fn = pal_numeric(colour_vector, domain = NULL)
  )
New York Temperature Measurements
Daily measurements in New York City (May 1-10, 1973)
Time Measurement
Year Month Day Temp,
°F
1973 5 1 67
1973 5 2 72
1973 5 3 74
1973 5 4 62
1973 5 5 56
1973 5 6 66
1973 5 7 65
1973 5 8 59
1973 5 9 61
1973 5 10 69

The colour mapping functions from palettes are: pal_numeric(), pal_bin(), pal_quantile(), and pal_factor(). These functions are useful when you need finer control over colour evaluation with data.