gt/data_color: scale color according to values in all columns, not per column?

Hello,

I would like to create a heatmap with the data_color function of the gt package.
However, it seems that the color values are actually calculated per column and not across
all cells of all the columns which have been selected.

See e.g. the attached table image:
chicken: size M income 65224.5 is lighter than size L sold 4932. And both chicken L sold (4932) and income (102339.0) have the same color value. I would think that this potentially misleading.

How could I scale the values across all columns?

Many thanks. r

library(tidyverse)
library(gt)

df_2 <-
  pizzaplace %>%
  dplyr::filter(
    type %in% c("chicken", "supreme")) %>%
  dplyr::group_by(type, size) %>%
  dplyr::summarize(
    sold = dplyr::n(),
    income = sum(price)
  ) 
#> `summarise()` regrouping output by 'type' (override with `.groups` argument)

names(df_2)[3:4]
#> [1] "sold"   "income"

df_2 %>%
  gt() %>% 
  data_color(
    columns = names(df_2)[3:4],
    colors = scales::col_numeric(
      palette = c("white", "#3fc1c9"),
      domain = NULL
    )
  )

Created on 2020-10-14 by the reprex package (v0.3.0)

gt_data_color

rather than domain= NULL , you can pass the domain as the full range of observed values.

full_val_range <- df_2 %>% 
  ungroup %>%
  select_if(is.numeric) %>% 
  range

df_2 %>%
  gt() %>% 
  data_color(
    columns =names(df_2)[3:4],
    colors = scales::col_numeric(
      palette = c("white", "#3fc1c9"),
      domain = full_val_range
    )
  )

image

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

many thanks for the quick response, excellent!