Beautiful tables in R with gtExtras

Merging static tables with graphics is a powerful combo

gt
tables
Author

Thomas Mock

Published

June 13, 2022

I’m very excited to have the first release of gtExtras available on CRAN!

The goal of gtExtras is to provide opinionated helper functions to assist in creating beautiful and functional tables with gt.

The functions are generally wrappers around boilerplate table-making code or adding opinionated functions like data journalism inspired table themes and inline graphics. The gt package is amazing, make sure to go read the official documentation.

For installation:

install.packages("gtExtras")
# or if wanting the dev version
# if needed install.packages("remotes")
remotes::install_github("jthomasmock/gtExtras")

Using gtExtras

Overall, there are a lot of available functions in gtExtras:

length(ls.str('package:gtExtras', mode='function'))
[1] 65

You can read about each of the functions in the function reference.

Overall, there are four families of functions in gtExtras:

  • Themes: 7 themes that style almost every element of a gt table, built off of data journalism-styled tables
  • Utilities: Helper functions for aligning/padding numbers, adding fontawesome icons, images, highlighting, dividers, styling by group, creating two tables or two column layouts, extracting ordered data from a gt table internals, or generating a random dataset for reprex
  • Plotting: 12 plotting functions for inline sparklines, win-loss charts, distributions (density/histogram), percentiles, dot + bar, bar charts, confidence intervals, or summarizing an entire dataframe!
  • Colors: 3 functions, a palette for “Hulk” style scale (purple/green), coloring rows with good defaults from paletteer, or adding a “color box” along with the cell value

Also see the Plotting with gtExtras article for more examples of combining tables and graphics together.

A subset of functions are included below, or see the full function reference.

Importantly, gtExtras is not at all a replacement for gt, but rather is almost a “cookbook” where common or repeated function calls are grouped into their own respective functions. At a technical level, gtExtras is literally just gt functions under the hood and I’ll highlight a few examples of how to do the same thing in each package.

Load libraries

Themes

The package includes seven different themes, and 3 examples are the gt_theme_538() styled after FiveThirtyEight style tables, the gt_theme_espn() styled after ESPN style tables, and the gt_theme_nytimes() styled after The New York Times tables.

head(mtcars) %>%
  gt() %>% 
  gt_theme_538() %>% 
  tab_header(title = "Table styled like the FiveThirtyEight")