It's possible to add color to data cells according to their values. The
data_color() function colors all rows of any
There are two ways to define how cells are colored: (1) through the use of a
supplied color palette, and (2) through use of a color mapping function
available from the
scales package. The first method colorizes cell
data according to whether values are character or numeric. The second method
provides more control over how cells are colored since we provide an explicit
color function and thus other requirements such as bin counts, cut points, or
a numeric domain. Finally, we can choose whether to apply the cell-specific
colors to either the cell background or the cell text.
data_color(data, columns, colors, alpha = NULL, apply_to = "bkgd", autocolor_text = TRUE)
a table object that is created using the
the columns wherein changes to cell data colors should occur.
either a color mapping function from the
an optional, fixed alpha transparency value that will be applied
to all of the
which style element should the colors be applied to? Options
include the cell background (the default, given as
an option to let gt modify the coloring of text
within cells undergoing background coloring. This will in some cases yield
more optimal text-to-background color contrast. By default, this is set to
an object of class
col_*() functions from the scales package can be used in the
colors argument. These functions map data values (
character) to colors according to the provided palette.
col_numeric(): provides a simple linear mapping
from continuous numeric data to an interpolated palette.
col_factor(): provides a mapping of factors to
colors. If the palette is discrete and has a different number of colors than
the number of factors, interpolation is used.
By default, gt will choose the ideal text color (for maximal contrast)
when colorizing the background of data cells. This option can be disabled by
Choosing the right color palette can often be difficult because it's both
hard to discover suitable palettes and then obtain the vector of colors. To
make this process easier we can elect to use the paletteer package,
which makes a wide range of palettes from various R packages readily
info_paletteer() information table allows us to
easily inspect all of the discrete color palettes available in
paletteer. We only then need to specify the
palette when calling the
and, we get the palette as a vector of hexadecimal colors.
# library(paletteer) # Use `countrypops` to create a gt table; # Apply a color scale to the `population` # column with `scales::col_numeric`, # four supplied colors, and a domain tab_1 <- countrypops %>% dplyr::filter(country_name == "Mongolia") %>% dplyr::select(-contains("code")) %>% tail(10) %>% gt() %>% data_color( columns = vars(population), colors = scales::col_numeric( palette = c( "red", "orange", "green", "blue"), domain = c(0.2E7, 0.4E7)) ) # Use `pizzaplace` to create a gt table; # Apply colors from the `red_material` # palette (in the `ggsci` pkg but # more easily gotten from the `paletteer` # package, info at `info_paletteer()`) to # to `sold` and `income` columns; setting # the `domain` of `scales::col_numeric()` # to `NULL` will use the bounds of the # available data as the domain tab_2 <- pizzaplace %>% dplyr::filter( type %in% c("chicken", "supreme")) %>% dplyr::group_by(type, size) %>% dplyr::summarize( sold = n(), income = sum(price) ) %>% gt(rowname_col = "size") %>% data_color( columns = vars(sold, income), colors = scales::col_numeric( palette = paletteer::paletteer_d( package = "ggsci", palette = "red_material" ), domain = NULL) )