
Add only a discrete color legend to a gt table
gtscale_color_discrete.RdThis is a lower-level helper for cases where table coloring is already
handled elsewhere. For the usual "color and legendize" workflow, prefer
gtscale_data_color_discrete().
Arguments
- data
A
gt_tblcreated bygt::gt().- values
A vector of color values or a single named discrete palette.
- labels
Labels for each color swatch. Defaults to
values.- title
Optional legend title.
- swatch_size
Size of each discrete color swatch.
Value
A modified gt::gt table.
Examples
library(gt)
data.frame(
category = c('Low', 'Medium', 'High'),
value = c(12, 47, 83)
) |>
gt() |>
data_color(
columns = category,
method = 'factor',
palette = c('#1b9e77', '#d95f02', '#7570b3')
) |>
gtscale_color_discrete(
values = c('#1b9e77', '#d95f02', '#7570b3'),
labels = c('Low', 'Medium', 'High'),
title = 'Category'
)
category
value
CategoryLowMediumHigh