The gt_color_rows
function takes an existing gt_tbl
object and
applies pre-existing palettes from the {paletteer}
package or custom
palettes defined by the user. This function is a custom wrapper around
gt::data_color()
, and uses some of the boilerplate code. Basic use
is simpler than data_color()
.
gt_color_rows(
gt_object,
columns,
palette = "ggsci::red_material",
direction = 1,
domain = NULL,
pal_type = c("discrete", "continuous"),
...
)
An object of class gt_tbl
.
An existing gt table object of class gt_tbl
The columns wherein changes to cell data colors should occur.
The colours or colour function that values will be mapped to
Either 1
or -1
. If -1
the palette will be reversed.
The possible values that can be mapped.
For col_numeric
and col_bin
, this can be a simple numeric
range (e.g. c(0, 100)
); col_quantile
needs representative
numeric data; and col_factor
needs categorical data.
If NULL
, then whenever the resulting colour function is called, the
x
value will represent the domain. This implies that if the function
is invoked multiple times, the encoding between values and colours may not
be consistent; if consistency is needed, you must provide a non-NULL
domain.
A string indicating the palette type (one of c("discrete", "continuous")
)
Additional arguments passed to scales::col_numeric()
library(gt)
# basic use
basic_use <- mtcars %>%
head(15) %>%
gt() %>%
gt_color_rows(mpg:disp)
# change palette to one that paletteer recognizes
change_pal <- mtcars %>%
head(15) %>%
gt() %>%
gt_color_rows(mpg:disp, palette = "ggsci::blue_material")
# change palette to raw values
vector_pal <- mtcars %>%
head(15) %>%
gt() %>%
gt_color_rows(
mpg:disp, palette = c("white", "green"))
# could also use palette = c("#ffffff", "##00FF00") # use discrete instead of continuous palette
discrete_pal <- mtcars %>%
head(15) %>%
gt() %>%
gt_color_rows(
cyl, pal_type = "discrete",
palette = "ggthemes::colorblind", domain = range(mtcars$cyl)
)
# use discrete and manually define range
range_pal <- mtcars %>%
dplyr::select(gear, mpg:hp) %>%
head(15) %>%
gt() %>%
gt_color_rows(
gear, pal_type = "discrete", direction = -1,
palette = "colorblindr::OkabeIto_black", domain = c(3,4,5))
4-2
Other Colors:
gt_color_box()
,
gt_hulk_col_numeric()