Skip to contents

Marks cells that fall outside a threshold, either beyond an interquartile fence, beyond a number of standard deviations, or outside bounds you supply.

Usage

gt_outliers(
  gt_object,
  columns,
  method = c("iqr", "sd", "bounds"),
  threshold = NULL,
  bounds = NULL,
  side = c("both", "high", "low"),
  fill = NULL,
  color = NULL,
  bold = TRUE,
  symbol = NULL,
  note = NULL
)

Arguments

gt_object

A gt table object to modify.

columns

The column or columns to test. Non-numeric columns are skipped.

method

Character. How to decide what counts as an outlier. One of "iqr", "sd", or "bounds". Defaults to "iqr".

threshold

Numeric. The cutoff used by "iqr" and "sd". If NULL, the convention for the method is used: 1.5 for "iqr" and 3 for "sd". Defaults to NULL.

bounds

A length-2 numeric vector giving c(lower, upper), required when method is "bounds". Use NA for an open end, such as c(NA, 100). Defaults to NULL.

side

Character. Which tail to flag. One of "both", "high", or "low". Defaults to "both".

fill

Optional. A hex color for the cell fill behind flagged values. Defaults to NULL, which applies no fill.

color

Optional. A hex color for flagged text. If NULL, a warning red is used, swapped for a readable alternative when it would not have enough contrast against fill. Defaults to NULL.

bold

Logical. Should flagged values be bolded? Defaults to TRUE.

symbol

Optional. A marker appended to flagged values, such as a dagger ("\u2020"). Defaults to NULL.

note

Optional. A source note describing the rule that was applied. Pass TRUE for wording generated from method and threshold, a string for your own, or NULL for no note. Defaults to NULL.

Value

Returns a modified gt table with outlying values marked.

Details

gt::data_color() shades a continuous scale; this flags a value as unusual. Thresholds are computed separately for each column, so every column is judged against its own distribution.

The default rule is the interquartile fence, not standard deviations, because an SD fence is built from a spread that the outlier itself inflates. On c(10.2, 10.4, 10.1, 19.8, 10.3, 10.0) the mean is 11.8 and the standard deviation 3.92, so a three-SD fence reaches 23.6 and misses the 19.8. The quartiles barely move, so the IQR fence stops at 10.75 and catches it. The masking is worst in small samples. Use method = "sd" if you want it anyway.

See also

gt_highlight_na() for missing values, and gt_spotlight() for drawing attention to whole rows.

Examples

library(gt)

nba <- subset(
  sdv_example_standings, league == "nba",
  c(team_name, points_for, points_against)
)

# the default fence (1.5 times the IQR) catches the league's lowest scorer
gt(nba) |> gt_outliers(c(points_for, points_against), note = TRUE)
team_name points_for points_against
Boston Celtics 9418 8787
New York Knicks 9549 9030
Toronto Raptors 9400 9168
Philadelphia 76ers 9502 9517
Brooklyn Nets 8686 9505
Detroit Pistons 9657 8988
Cleveland Cavaliers 9801 9464
Milwaukee Bucks 9072 9581
Chicago Bulls 9537 9964
Indiana Pacers 9219 9874
Atlanta Hawks 9714 9516
Orlando Magic 9491 9439
Charlotte Hornets 9513 9117
Miami Heat 9911 9720
Washington Wizards 9258 10240
Oklahoma City Thunder 9760 8846
Denver Nuggets 10010 9588
Minnesota Timberwolves 9676 9401
Portland Trail Blazers 9469 9493
Utah Jazz 9642 10333
Los Angeles Lakers 9540 9396
Phoenix Suns 9232 9112
LA Clippers 9329 9236
Golden State Warriors 9398 9444
Sacramento Kings 9102 9922
San Antonio Spurs 9826 9145
Houston Rockets 9449 9021
New Orleans Pelicans 9473 9842
Dallas Mavericks 9358 9810
Memphis Grizzlies 9403 9896
Marked values fall outside 1.5 × IQR of the column quartiles.
# an explicit acceptance range, flagging only the high side gt(nba) |> gt_outliers(c(points_for, points_against), method = "bounds", bounds = c(NA, 9800), side = "high", fill = "#FDECEA", symbol = "\u2020" )
team_name points_for points_against
Boston Celtics 9418 8787
New York Knicks 9549 9030
Toronto Raptors 9400 9168
Philadelphia 76ers 9502 9517
Brooklyn Nets 8686 9505
Detroit Pistons 9657 8988
Cleveland Cavaliers 9801† 9464
Milwaukee Bucks 9072 9581
Chicago Bulls 9537 9964†
Indiana Pacers 9219 9874†
Atlanta Hawks 9714 9516
Orlando Magic 9491 9439
Charlotte Hornets 9513 9117
Miami Heat 9911† 9720
Washington Wizards 9258 10240†
Oklahoma City Thunder 9760 8846
Denver Nuggets 10010† 9588
Minnesota Timberwolves 9676 9401
Portland Trail Blazers 9469 9493
Utah Jazz 9642 10333†
Los Angeles Lakers 9540 9396
Phoenix Suns 9232 9112
LA Clippers 9329 9236
Golden State Warriors 9398 9444
Sacramento Kings 9102 9922†
San Antonio Spurs 9826† 9145
Houston Rockets 9449 9021
New Orleans Pelicans 9473 9842†
Dallas Mavericks 9358 9810†
Memphis Grizzlies 9403 9896†
# works on wider data too nfl <- subset( sdv_example_standings, league == "nfl", c(team_name, wins, points_for, points_against) ) gt(nfl) |> gt_outliers(c(wins, points_for, points_against))
team_name wins points_for points_against
New England Patriots 14 490 320
Buffalo Bills 12 481 365
Miami Dolphins 7 347 424
New York Jets 3 300 503
Pittsburgh Steelers 10 397 387
Baltimore Ravens 8 424 398
Cincinnati Bengals 6 414 492
Cleveland Browns 5 279 379
Jacksonville Jaguars 13 474 336
Houston Texans 12 404 295
Indianapolis Colts 8 466 412
Tennessee Titans 3 284 478
Denver Broncos 14 401 311
Los Angeles Chargers 11 368 340
Kansas City Chiefs 6 362 328
Las Vegas Raiders 3 241 432
Philadelphia Eagles 11 379 325
Dallas Cowboys 7 471 511
Washington Commanders 5 356 451
New York Giants 4 381 439
Chicago Bears 11 441 415
Green Bay Packers 9 391 360
Minnesota Vikings 9 344 333
Detroit Lions 9 481 413
Carolina Panthers 8 311 380
Tampa Bay Buccaneers 8 380 411
Atlanta Falcons 8 353 401
New Orleans Saints 6 306 383
Seattle Seahawks 14 483 292
Los Angeles Rams 12 518 346
San Francisco 49ers 12 437 371
Arizona Cardinals 3 355 488