── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gt)library(palmerpenguins)
Attaching package: 'palmerpenguins'
The following objects are masked from 'package:datasets':
penguins, penguins_raw
library(scales)
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
Basic table Select a handful of columns and render a simple gt table.
Warning: Since gt v0.9.0, the `colors` argument has been deprecated.
• Please use the `fn` argument instead.
This warning is displayed once every 8 hours.
Warning: Since gt v0.3.0, `columns = vars(...)` has been deprecated.
• Please use `columns = c(...)` instead.
species
island
bill_length_mm
bill_depth_mm
flipper_length_mm
Body mass (g)
sex
Adelie
Torgersen
39.1
18.7
181
3,750
male
Adelie
Torgersen
39.5
17.4
186
3,800
female
Adelie
Torgersen
40.3
18.0
195
3,250
female
Adelie
Torgersen
36.7
19.3
193
3,450
female
Adelie
Torgersen
39.3
20.6
190
3,650
male
Adelie
Torgersen
38.9
17.8
181
3,625
female
Adelie
Torgersen
39.2
19.6
195
4,675
male
Adelie
Torgersen
41.1
17.6
182
3,200
female
Adelie
Torgersen
38.6
21.2
191
3,800
male
Adelie
Torgersen
34.6
21.1
198
4,400
male
Using scales to create the colour mapper The function col_numeric() used above comes from the scales package (scales::col_numeric). It builds and returns a function that maps numeric values to colors; you pass that function to gt::data_color so gt can colour cells based on the numeric values. The two important arguments are palette (the colour stops — use hex codes or a perceptually uniform palette like viridis) and domain (the numeric range to map).
Tip: construct the mapping function explicitly so you can inspect or reuse it, and prefer explicit domains (range(…, na.rm = TRUE)) and hex colours for reproducibility and accessibility.
# create a reusable colour-mapping function (example using viridis)library(viridisLite) # lightweight dependency for perceptual colourscolor_fn <- scales::col_numeric(palette = viridisLite::viridis(5), # perceptually uniform palettedomain =range(penguins_clean$body_mass_g, na.rm =TRUE) # explicit numeric domain)# inspect output for a few valuescolor_fn(c(2700, 3500, 5000))
[1] "#440154" "#3E4B85" "#4BAF77"
# use the function with gt::data_colorpenguins_clean %>%slice_head(n =10) %>%gt() %>%cols_label(body_mass_g ="Body mass (g)") %>%data_color(columns =c("body_mass_g"), colors = color_fn) %>%fmt_number(columns =vars(body_mass_g), decimals =0)
Warning: Since gt v0.3.0, `columns = vars(...)` has been deprecated.
• Please use `columns = c(...)` instead.
species
island
bill_length_mm
bill_depth_mm
flipper_length_mm
Body mass (g)
sex
Adelie
Torgersen
39.1
18.7
181
3,750
male
Adelie
Torgersen
39.5
17.4
186
3,800
female
Adelie
Torgersen
40.3
18.0
195
3,250
female
Adelie
Torgersen
36.7
19.3
193
3,450
female
Adelie
Torgersen
39.3
20.6
190
3,650
male
Adelie
Torgersen
38.9
17.8
181
3,625
female
Adelie
Torgersen
39.2
19.6
195
4,675
male
Adelie
Torgersen
41.1
17.6
182
3,200
female
Adelie
Torgersen
38.6
21.2
191
3,800
male
Adelie
Torgersen
34.6
21.1
198
4,400
male
For other needs, scales also provides helpers like col_bin(), col_quantile(), and col_factor() to create binned, quantile-based, or categorical colour mappings. ``````
Grouped summary (per species) Create a summary table grouped by species: mean body mass and median flipper length.