Skip to contents

A shorthand around gt::data_color() for filling columns that already hold rank values, where 1 is best. It takes either a plain vector of hex colors or a paletteer palette, and either an explicit domain or one derived from the data. Values are colored as they are; no ranking is computed for you.

Usage

gt_color_ranks(
  gt_object,
  columns,
  rows = NULL,
  palette = c("#3D8B6E", "#9DC5A7", "#EDE0CC", "#DB9070", "#BE4D3A"),
  domain = NULL,
  reverse = FALSE,
  na_color = "white",
  autocolor_text = TRUE,
  pal_type = "discrete",
  ...
)

Arguments

gt_object

A gt table object to modify.

columns

The column or columns to color.

rows

The rows to color. Either an expression evaluated against the table's data, such as wins > 10, or a numeric vector of row indices. Rows left out keep their default background. If NULL, every row is colored. Defaults to NULL.

palette

A color palette to use. If you want a palette from paletteer, specify it as package::palette. Defaults to a five-color green-to-red ramp.

domain

A length-2 numeric vector giving the value range mapped onto the palette. If NULL, the range is taken from the selected columns and shared across all of them, so rank 1 and the largest rank present anchor the two ends of the palette. Defaults to NULL.

reverse

Logical. Should the palette be reversed? Defaults to FALSE.

na_color

Character. The color used for NA values. Defaults to "white".

autocolor_text

Logical. Should the text color be set automatically for contrast against the fill? Defaults to TRUE.

pal_type

Character. Either "discrete" or "continuous", used when applying paletteer palettes. Defaults to "discrete".

...

Additional arguments passed to gt::data_color.

Value

Returns a modified gt table with the selected columns filled by value.

Details

The whole cell background is filled. gt_color_pills() does the same job with rounded pill spans instead.

See also

gt_legend_continuous() for explaining the scale, and gt_color_pills() for a pill treatment instead of a full-cell fill.

Examples

library(gt)

west <- subset(sdv_example_standings, league == "nba" & conference == "Western")
ranked <- data.frame(
  team = west$team_name,
  offense_rank = rank(-west$points_for, ties.method = "min"),
  defense_rank = rank(west$points_against, ties.method = "min")
)

gt(ranked) |> gt_color_ranks(c(offense_rank, defense_rank))
team offense_rank defense_rank
Oklahoma City Thunder 3 1
Denver Nuggets 1 10
Minnesota Timberwolves 4 7
Portland Trail Blazers 8 9
Utah Jazz 5 15
Los Angeles Lakers 6 6
Phoenix Suns 14 3
LA Clippers 13 5
Golden State Warriors 11 8
Sacramento Kings 15 14
San Antonio Spurs 2 4
Houston Rockets 9 2
New Orleans Pelicans 7 12
Dallas Mavericks 12 11
Memphis Grizzlies 10 13
# a paletteer palette, reversed if (requireNamespace("paletteer", quietly = TRUE)) { gt(ranked) |> gt_color_ranks(offense_rank, palette = "viridis::mako", pal_type = "continuous", reverse = TRUE ) }
team offense_rank defense_rank
Oklahoma City Thunder 3 1
Denver Nuggets 1 10
Minnesota Timberwolves 4 7
Portland Trail Blazers 8 9
Utah Jazz 5 15
Los Angeles Lakers 6 6
Phoenix Suns 14 3
LA Clippers 13 5
Golden State Warriors 11 8
Sacramento Kings 15 14
San Antonio Spurs 2 4
Houston Rockets 9 2
New Orleans Pelicans 7 12
Dallas Mavericks 12 11
Memphis Grizzlies 10 13