Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Last active October 13, 2021 03:51
Show Gist options
  • Save rich-iannone/1da1ae7a7203958a0c5b1bd1d4b24017 to your computer and use it in GitHub Desktop.
Save rich-iannone/1da1ae7a7203958a0c5b1bd1d4b24017 to your computer and use it in GitHub Desktop.
This gt code allows you to make a summary table based on the `pizzaplace` dataset.
library(tidyverse)
library(paletteer)
library(gt)
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" ~ "veggie (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()
@tvedebrink
Copy link

I guess you want the fourth label to read:
"veggie" ~ "veggie (pizzas without any meats whatsoever)"
as 🐔 seems out of place here 😄

@rich-iannone
Copy link
Author

I guess you want the fourth label to read:
"veggie" ~ "veggie (pizzas without any meats whatsoever)"
as 🐔 seems out of place here 😄

Thanks for catching this! Will modify now.

@RNPinho
Copy link

RNPinho commented Jan 8, 2021

How you setted the alignment of the "sold" and "income" labels to right??

Copy link

ghost commented Jan 12, 2021

How you setted the alignment of the "sold" and "income" labels to right??

I think you need a row in your code like this:

tab_style(locations = cells_column_labels(columns = TRUE), style = cell_text(align = "right")) %>%

@ChrystalaPsathiti
Copy link

Is there any way to export the table as a png file?

@rich-iannone
Copy link
Author

Is there any way to export the table as a png file?

Yes! You can use the gtsave() function and provide a file name ending with .png. For example: gtsave(gt_tbl, filename = "pizzaplace.png").

@ChrystalaPsathiti
Copy link

Is there any way to export the table as a png file?

Yes! You can use the gtsave() function and provide a file name ending with .png. For example: gtsave(gt_tbl, filename = "pizzaplace.png").

Thank you so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment