It's possible to add color to data cells according to their values. The
data_color() function colors all rows of any
columns supplied. 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.
1 2 3 4 5 6 7 8
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 scales package or
a vector of colors to use for each distinct value or level in each of 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
col_*() color mapping functions from the scales package can be used in
colors argument. These functions map data values (
character) to colors according to the provided palette.
scales::col_numeric(): provides a simple linear mapping from
continuous numeric data to an interpolated palette.
scales::col_bin(): provides a mapping of continuous numeric data to
value-based bins. This internally uses the
scales::col_quantile(): provides a mapping of continuous
numeric data to quantiles. This internally uses the
scales::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
paletteer::paletteer_d() function, and, we get the palette as a vector of
An object of class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 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 = dplyr::n(), income = sum(price) ) %>% gt(rowname_col = "size") %>% data_color( columns = vars(sold, income), colors = scales::col_numeric( palette = paletteer::paletteer_d( palette = "ggsci::red_material" ) %>% as.character(), domain = NULL ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.