[R] Riding tables with {gt} and {gtExtras}

The {gt} extension already allowed to easily create tables from raw dataset, but now the {gtExtras} extension adds many customization options. Here we will illustrate the possibilities of these packages with TidyTuesday dataset on Tour de France riders, extracted from Alastair Rushworth’s {tdf} extension.

1. Tables with text

We will start by loading the data.

library(tidyverse)
# Load data: 
# Whole race winners
tdf_winners <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-07/tdf_winners.csv')
# Stages winners
tdf_stages <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-07/tdf_stages.csv')

head(tdf_stages)
## # A tibble: 6 x 8
##   Stage Date       Distance Origin            Destination Type  Winner Winner_Country
##   <chr> <date>        <dbl> <chr>             <chr>       <chr> <chr>  <chr>         
## 1 1     2017-07-01      14  Düsseldorf        Düsseldorf  Indi~ Gerai~ GBR           
## 2 2     2017-07-02     204. Düsseldorf        Liège       Flat~ Marce~ GER           
## 3 3     2017-07-03     212. Verviers          Longwy      Medi~ Peter~ SVK           
## 4 4     2017-07-04     208. Mondorf-les-Bains Vittel      Flat~ Arnau~ FRA           
## 5 5     2017-07-05     160. Vittel            La Planche~ Medi~ Fabio~ ITA           
## 6 6     2017-07-06     216  Vesoul            Troyes      Flat~ Marce~ GER

As a first example, we will make a table with the stage winners of a given year: 1961. We will see later why I chose this year…

tdf_61 <- tdf_stages%>%
  # Create year column from Date
  mutate(year = lubridate::year(Date))%>%
  # Keep only 1961 data
  filter(year==1961)%>%
  # Remove year column
  select(-year)

We will now load the {gt} package to format our first table. One of the main advantages of {gt} is that it fits perfectly into tidyverse pipes: with just have to add the gt() function to create a table.

# Load {gt}
library(gt)

# Make table with gt()
tab<-tdf_61 %>%
  # Keep only first 6 rows as example
  head(6)%>%
  # Make table
  gt() 

tab
Stage Date Distance Origin Destination Type Winner Winner_Country
1a 1961-06-25 136.5 Rouen Versailles Plain stage André Darrigade FRA
1b 1961-06-25 28.5 Versailles Versailles Individual time trial Jacques Anquetil FRA
2 1961-06-26 230.5 Pontoise Roubaix Plain stage André Darrigade FRA
3 1961-06-27 197.5 Roubaix Charleroi Plain stage Emile Daems BEL
4 1961-06-28 237.5 Charleroi Metz Plain stage Anatole Novak FRA
5 1961-06-29 221.0 Metz Strasbourg Stage with mountain(s) Louis Bergaud FRA

Once the table is created, we can use the pipe operator %>% to add few functions to customize the table. Here are some examples of how to add a title or format the columns:

tab<-tab %>%
  # Add title and subtitle
  tab_header(
    title = "Stage winners",
    # Use markdown syntax with md()
    subtitle = md("Tour de France **1961**")
  )%>%
  # Fomat date without year information
  fmt_date(
    columns = Date,
    date_style = 9
  )%>%
  # Format distance without decimal 
  fmt_number(
    columns = Distance,
    decimals = 0,
    # Add 'km' as suffix
    pattern = "{x} km"
  )%>%
  # Rename column
  cols_label(
    Winner_Country = "Nationality"
  )

tab
Stage winners
Tour de France 1961
Stage Date Distance Origin Destination Type Winner Nationality
1a 25 juin 136 km Rouen Versailles Plain stage André Darrigade FRA
1b 25 juin 28 km Versailles Versailles Individual time trial Jacques Anquetil FRA
2 26 juin 230 km Pontoise Roubaix Plain stage André Darrigade FRA
3 27 juin 198 km Roubaix Charleroi Plain stage Emile Daems BEL
4 28 juin 238 km Charleroi Metz Plain stage Anatole Novak FRA
5 29 juin 221 km Metz Strasbourg Stage with mountain(s) Louis Bergaud FRA

If you like customization, the {gt} extension allows to change the appearance of each cell (color, background, font, border…). Using the tab_style() function, you may customize the title as follows:

tab %>%
  tab_style(
    # Select object to modify
    locations = cells_title(groups = 'title'),
    # Specify text style
    style = list(
      cell_text(
        font=google_font(name = 'Bebas Neue'), 
        size='xx-large',
        color='indianred'
  )))
Stage winners
Tour de France 1961
Stage Date Distance Origin Destination Type Winner Nationality
1a 25 juin 136 km Rouen Versailles Plain stage André Darrigade FRA
1b 25 juin 28 km Versailles Versailles Individual time trial Jacques Anquetil FRA
2 26 juin 230 km Pontoise Roubaix Plain stage André Darrigade FRA
3 27 juin 198 km Roubaix Charleroi Plain stage Emile Daems BEL
4 28 juin 238 km Charleroi Metz Plain stage Anatole Novak FRA
5 29 juin 221 km Metz Strasbourg Stage with mountain(s) Louis Bergaud FRA

You may now try to customize a few parts of the table according to your taste! To help you select the elements you want to modify, here is a table summarizing the different parts of the table:

{gt} tables structure (Image credit: Introduction to creating {gt} tables)

You need to specify the name of the element of the table that you want to modifiy, after “cells_”, in the locations argument. These helper functions to target the location cells associated with the styling are summarized here.

But, as you can see, it can be tedious to modify each part of the table in this way. Fortunately, the new {gtExtras} extension allows to use predefined themes.

# Install gtExtras
# remotes::install_github("jthomasmock/gtExtras")

# Load extension
library(gtExtras)
# Apply 'New York Times' theme
tab<-tab%>%
  gtExtras::gt_theme_nytimes()

tab
Stage winners
Tour de France 1961
Stage Date Distance Origin Destination Type Winner Nationality
1a 25 juin 136 km Rouen Versailles Plain stage André Darrigade FRA
1b 25 juin 28 km Versailles Versailles Individual time trial Jacques Anquetil FRA
2 26 juin 230 km Pontoise Roubaix Plain stage André Darrigade FRA
3 27 juin 198 km Roubaix Charleroi Plain stage Emile Daems BEL
4 28 juin 238 km Charleroi Metz Plain stage Anatole Novak FRA
5 29 juin 221 km Metz Strasbourg Stage with mountain(s) Louis Bergaud FRA

Seven themes are available with {gtExtras}.

We are almost done with this first table. Now it is time to see why I select the stage winnrs of year 1961 as an example. With gt_highlight_rows(), we may highlight the name of one rider.

tab%>%
  gtExtras::gt_highlight_rows(
    # Row to highlight
    rows = 5, 
    # Background color
    fill = "lightgrey",
    # Bold for target column only
    bold_target_only = TRUE,
    # Select target column
    target_col = Winner
  )