Making Tables

code

A quick review of the gt package for making tables

I’ve wanted to dive into the gt package for making tables for awhile now. Here is a quick example of a table using data from a tidy tuesday a few weeks back. It seems my hugo theme is over-writing some of the table css, I’ll have to check that out.

Let’s read in the cocktails recipie

Show code
boston_cocktails <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-26/boston_cocktails.csv') %>% 
  janitor::clean_names()

Let’s make the data reasonable!

boston_tidy <- boston_cocktails %>% 
  # fix the measurements
  separate(measure, into = c('value', 'measurement'), sep = " ", remove = FALSE) %>% 
  # remove values that don't have properly formed measurements crudely
  filter(measurement == "oz") %>% 
  # convert fractions to decimals
  mutate(value = case_when(
    value == "1/2" ~ ".5",
    value == "1/3" ~ ".33",
    value == "1/4" ~ ".25",
    value == "3/4" ~ ".74",
    TRUE ~ value
  )) %>% 
  # magic
  type_convert() %>% 
  # only keep realistic values
  filter(value <= 5)

boston_table <- boston_tidy%>% 
  filter(!category %in% c('Non-alcoholic Drinks', 'Rum', 'Shooters')) %>% 
  group_by(category, name) %>% 
  summarize(total_ingredients = which.max(ingredient_number),
            total_ounces = sum(value)) %>% 
  arrange(category, desc(total_ingredients)) %>% 
  slice_head(n = 5) %>% 
  arrange(category) %>% 
  ungroup()

make the table

boston_gt_table <- gt(boston_table, 
                      rowname_col = "name",
                      groupname_col = "category")


boston_gt_table <- boston_gt_table %>% 
  tab_header(
    title = "Top cocktails by Category",
    subtitle = "Top 5 cocktails based on number of ingredients"
  ) %>% 
  tab_spanner(label = "Drink Information",
              columns = vars("total_ingredients", "total_ounces")) %>% 
  cols_label(
    total_ingredients = "Total Ingredients",
    total_ounces = "Total Ounces",
    name = "Name"
  ) %>% 
  summary_rows(
    groups = TRUE,
    columns = vars(total_ingredients, total_ounces),
    fns = list(Total = "sum"),
    formatter = fmt_number,
    decimals = 2,
    use_seps = TRUE
  ) 

boston_gt_table %>% 
  tab_options(table.width = "100%",
              table.font.size = "smaller",
              data_row.padding = "3px") %>% 
  tab_source_note(
    source_note = md("Data comes from the [Tidy Tuesday project](https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-05-26/readme.md)")
  ) %>% 
  opt_align_table_header(align = "center")
Top cocktails by Category
Top 5 cocktails based on number of ingredients
Drink Information
Total Ingredients Total Ounces
Brandy
Champs Elysees Cocktail 5 4.00
Cherry Blossom 5 2.50
D'artagnan 5 6.50
Deauville Cocktail 5 3.00
Gilroy Cocktail 5 3.48
Total 25.00 19.48
Cocktail Classics
Eye-Opener 6 6.50
Gloom Lifter 6 4.00
Hyatt's Jamaican Banana 6 5.50
New Orleans Gin Fizz 6 6.00
Prairie Oyster Cocktail 6 6.00
Total 30.00 28.00
Cordials and Liqueurs
Praire Oyster Cocktail 5 5.00
Absinthe Special Cocktail 2 1.25
Amaretto Rose 2 1.00
Amaretto Sour 2 1.24
Amber Amour 2 0.50
Total 13.00 8.99
Gin
The Winkle 6 11.00
Vow Of Silence 5 2.24
The Wink 4 4.50
Water Lily 4 2.96
Western Rose 4 3.00
Total 23.00 23.70
Rum - Daiquiris
Hai Karate 6 7.00
Ko Adang 6 5.00
Zombie 6 7.00
Floridita No. 3 5 4.24
Hush and Wonder 5 7.48
Total 28.00 30.72
Tequila
Amante Picante 6 10.00
Chupa Cabra 6 5.24
Flower Power 6 9.50
Oldest Temptation 6 5.25
The Nomad South 6 5.50
Total 30.00 35.49
Vodka
Ibiza 6 5.50
Long Island Iced Tea 6 4.20
Purple Passion Iced Tea 6 3.24
Sonic Blaster 6 4.50
Thyme Collins 6 10.00
Total 30.00 27.44
Whiskies
Blarney Stone Cocktail 6 5.50
California Lemonade 6 6.25
Carre Reprise 6 5.50
Chi-Town Flip 6 5.47
Double Standard Sour 6 3.73
Total 30.00 26.45
Data comes from the Tidy Tuesday project

More to come!

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/colemanrob/robcoleman.ca, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".