Creating publication quality tables using gt in rstats

Almost every project involves an explanation of the output to convey the impact / utility of the work done.
Tables and Visualizations are perhaps the most preferred ways of communication.
The gt
package from RStudio simplifies the job of creating great looking tables!
The name gt is short for “grammar of tables” and the goal of gt is similar to that of ggplot2, serving to not just to make it easy to make specific tables, but to describe a set of underlying components that can be recombined in different ways to solve different problems
The below table is such a table completely generated using R.
🍕 Pizzas Sold in 2015 🍕 | ||
---|---|---|
chicken (pizzas with chicken as a major ingredient) | ||
chicken (pizzas without any meats whatsoever) | ||
classic (classical pizzas)1 | ||
supreme (pizzas that try a little harder) | ||
1
The pizza category with the highest total sales.
2
Custom sizes for The Greek pizza.
3
This is a new record. Truly, 2015 was a great year for the |
The code for generating the same using gt
is:
setwd("~/Dropbox/pandora/My-Projects/repos/diary/writing/")
# credits:
# [Great Looking Tables: gt (v0.2) | RStudio Blog](https://blog.rstudio.com/2020/04/08/great-looking-tables-gt-0-2/)
# [RStudio Cloud](https://rstudio.cloud/project/779965)
# [This gt code allows you to make a summary table based on the `pizzaplace` dataset.](https://gist.github.com/rich-iannone/1da1ae7a7203958a0c5b1bd1d4b24017)
# [rstudio/gt: Easily generate information-rich, publication-quality tables from R](https://github.com/rstudio/gt)
library(tidyverse)
# devtools::install_github("EmilHvitfeldt/paletteer")
library(paletteer)
# remotes::install_github("rstudio/gt")
library(gt)
library(glue)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# create table on pizzaz
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pizzaplace %>%
mutate(type = case_when(
type == "chicken" ~ "chicken (pizzas with chicken as a major ingredient)",
type == "classic" ~ "classic (classical pizzas)",
type == "supreme" ~ "supreme (pizzas that try a little harder)",
type == "veggie" ~ "chicken (pizzas without any meats whatsoever)",
)) %>%
mutate(size = factor(size, levels = c("S", "M", "L", "XL", "XXL"))) %>%
dplyr::group_by(type, size) %>%
dplyr::summarize(
sold = n(),
income = sum(price)
) %>%
gt(rowname_col = "size") %>%
tab_header(title = md("🍕 Pizzas Sold in 2015 🍕")) %>%
fmt_number(
columns = vars(sold),
decimals = 0,
use_seps = TRUE
) %>%
fmt_currency(
columns = vars(income),
currency = "USD"
) %>%
cols_align(align = "right", columns = TRUE) %>%
data_color(
columns = vars(sold, income),
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
),
alpha = 0.8
) %>%
summary_rows(
groups = TRUE,
columns = vars(sold),
fns = list(TOTAL = "sum"),
formatter = fmt_number,
decimals = 0,
use_seps = TRUE
) %>%
summary_rows(
groups = TRUE,
columns = vars(income),
fns = list(TOTAL = "sum"),
formatter = fmt_currency,
currency = "USD"
) %>%
grand_summary_rows(
columns = vars(sold),
fns = list(`GRAND TOTAL` = "sum"),
formatter = fmt_number,
decimals = 0,
use_seps = TRUE
) %>%
grand_summary_rows(
columns = vars(income),
fns = list(`GRAND TOTAL` = "sum"),
formatter = fmt_currency,
currency = "USD"
) %>%
tab_footnote(
footnote = "The pizza category with the highest total sales.",
locations = cells_row_groups("classic (classical pizzas)")
) %>%
tab_footnote(
footnote = md("Custom sizes for **The Greek** pizza."),
locations = cells_stub(c("XL", "XXL"))
) %>%
tab_footnote(
footnote = md("This is a new record. Truly, 2015 was a **great** year for the `pizzaplace`."),
locations = cells_grand_summary(columns = vars(sold))
) %>%
tab_options(
summary_row.background.color = "#ACEACE80",
grand_summary_row.background.color = "#990000",
row_group.background.color = "#FFEFDB80",
heading.background.color = "#EFFBFC",
column_labels.background.color = "#EFFBFC",
stub.background.color = "#EFFBFC",
table.font.color = "#323232",
table_body.hlines.color = "#989898",
table_body.border.top.color = "#989898",
heading.border.bottom.color = "#989898",
row_group.border.top.color = "#989898",
row_group.border.bottom.style = "none",
stub.border.style = "dashed",
stub.border.color = "#989898",
stub.border.width = "1px",
summary_row.border.color = "#989898",
table.width = "60%"
) %>%
opt_all_caps()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~