gt - a (G)rammar of (T)ables

NFL tidyverse tables

Not to be confused with a Game of Thrones

Thomas Mock
05-18-2020
Preview of the table to come

gt package - a (g)rammar of (t)ables

gt is an R package for generating formatted tables from dataframes in R with a Grammar of Tables.

If you want to go deeper than this basic guide, check out the gt site, which has lots of examples!

Raw data comes from: Pro Football Reference & Over the Cap

Component parts of a gt table

Per the package website, gt has the following component parts:

The parts (roughly from top to bottom) are:

As you can see it is fleshing out the idea of formatting or adding various parts of the table in a robust way.


Read in the Data

I’ve gone through collecting the data and have put into a non-tidy wide format for Salary Rank, playoff week and appearances, Total appearances, and finally salary from 2014-2019.


library(gt) # for static tables
library(tidyverse) # all the things
library(paletteer) # for all the palettes

playoff_salary <- read_csv("https://raw.githubusercontent.com/jthomasmock/radix_themockup/master/_posts/2020-05-13-qb-salaries-vs-playoff-appearances/playoff_salary.csv")

glimpse(playoff_salary)

Rows: 36
Columns: 7
$ player     <chr> "Tom Brady", "Aaron Rodgers", "Russell Wilson", …
$ Wildcard   <dbl> 1, 2, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 0, 0, …
$ Division   <dbl> 5, 4, 4, 3, 2, 2, 2, 2, 1, 2, 1, 1, 2, 0, 2, 2, …
$ Conference <dbl> 5, 3, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 2, 1, …
$ Superbowl  <dbl> 4, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, …
$ Total      <dbl> 15, 9, 10, 7, 4, 5, 6, 3, 3, 5, 3, 3, 5, 2, 5, 4…
$ salary     <dbl> 100.065, 125.602, 91.114, 127.690, 93.700, 75.26…

Basics of gt

A very basic gt table can be created as so:


playoff_salary %>%
  head() %>%
  gt()
player Wildcard Division Conference Superbowl Total salary
Tom Brady 1 5 5 4 15 100.065
Aaron Rodgers 2 4 3 0 9 125.602
Russell Wilson 4 4 1 1 10 91.114
Ben Roethlisberger 3 3 1 0 7 127.690
Alex Smith 2 2 0 0 4 93.700
Andrew Luck 2 2 1 0 5 75.264

Immediately we have a basic table with minimal formatting.

The core parts we want to change are:
- Conditional color formatting for Total Appearances and Salary
- Change the fonts
- Add borders

Conditional Colors

We covered conditional colors in the previous post - check it out if you have further questions about building or using palettes.

A MAJOR value add that gt provides is rather than having to write our own palette parser it has a built in one via gt::data_color()! Just to refresh us on our palette, we’re using an adaptation of the viridis palette.


scales::show_col(c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab"))

For today’s example, we’ll use one from paletteer as well! Notice that printing paletteer::d() directly attaches the colors in the console, and to use it as a plain old character vector, we’ll need to wrap it in as.character() OR pipe it forward like palette %>% as.character().


paletteer::paletteer_d("ggsci::red_material", n = 6) %>%
  as.character() %>%
  scales::show_col()

I’d also like to introduce a function to generate colors from the scales package. scales::col_numeric() can generate continuous color scales based off a vector of colors. Notice that it operates differently than our other color function that is normalized between 0 and 1. This one takes ANY number and should be used with a vector of numbers. Notice the behavior with single colors generates the same color every time (nothing to scale against since we used domain = NULL).


red_color_generator <- scales::col_numeric(c("red", "white"), domain = NULL)

red_color_generator(50) %>% scales::show_col()