Skip to contents

Shades every other row group, so each group reads as a block. gt::opt_row_striping() bands every other row, which fights the grouping on a table that is already divided into sections.

Usage

gt_group_stripes(gt_object, color = "#F5F5F5", start = 2, include_stub = TRUE)

Arguments

gt_object

A gt table object to modify. It must have row groups.

color

Character. A hex color for the banded groups. Defaults to "#F5F5F5".

start

Integer. Which group to begin banding on, in the order the groups are rendered. 2 leaves the first group unshaded, 1 shades it. Defaults to 2.

include_stub

Logical. Should the stub column be banded along with the body? Defaults to TRUE.

Value

Returns a modified gt table with alternate groups banded.

Details

Groups are banded in the order they render, so this follows gt::row_group_order() rather than the order the groups happen to appear in the data.

The fill is applied to body cells rather than through CSS, because a group heading occupies a row of its own and shifts every nth-child count below it. Group heading rows are left alone; gt::tab_options(row_group.background.color) sets those, and it sets all of them at once.

Examples

library(gt)

afc <- subset(
  sdv_example_standings, conference == "AFC",
  c(team_name, division, wins, losses, points_for)
)

gt(afc, groupname_col = "division") |>
  gt_theme_broadsheet() |>
  gt_group_stripes()
team_name wins losses points_for
AFC East
New England Patriots 14 3 490
Buffalo Bills 12 5 481
Miami Dolphins 7 10 347
New York Jets 3 14 300
AFC North
Pittsburgh Steelers 10 7 397
Baltimore Ravens 8 9 424
Cincinnati Bengals 6 11 414
Cleveland Browns 5 12 279
AFC South
Jacksonville Jaguars 13 4 474
Houston Texans 12 5 404
Indianapolis Colts 8 9 466
Tennessee Titans 3 14 284
AFC West
Denver Broncos 14 3 401
Los Angeles Chargers 11 6 368
Kansas City Chiefs 6 11 362
Las Vegas Raiders 3 14 241
# shade from the first group instead, in a warmer tone gt(afc, groupname_col = "division") |> gt_group_stripes(color = "#FBF3E4", start = 1)
team_name wins losses points_for
AFC East
New England Patriots 14 3 490
Buffalo Bills 12 5 481
Miami Dolphins 7 10 347
New York Jets 3 14 300
AFC North
Pittsburgh Steelers 10 7 397
Baltimore Ravens 8 9 424
Cincinnati Bengals 6 11 414
Cleveland Browns 5 12 279
AFC South
Jacksonville Jaguars 13 4 474
Houston Texans 12 5 404
Indianapolis Colts 8 9 466
Tennessee Titans 3 14 284
AFC West
Denver Broncos 14 3 401
Los Angeles Chargers 11 6 368
Kansas City Chiefs 6 11 362
Las Vegas Raiders 3 14 241