Example 1: wine_ratings

wine_table <- wine_ratings %>% 
  group_by(variety) %>% 
  summarize(mean_price = mean(price, na.rm = TRUE),
            mean_rating = mean(points, na.rm = TRUE),
            wines_rated = n())

wine_table %>% 
  gt()
variety mean_price mean_rating wines_rated
Cabernet Sauvignon 48.25220 88.61731 8840
Chardonnay 34.70414 88.32306 10868
Pinot Noir 47.87520 89.42654 12278
wine_table %>% 
  gt() %>% 
  tab_header(title = "Most Popular Wine Varieties",
             subtitle = "Rating and Price")
Most Popular Wine Varieties
Rating and Price
variety mean_price mean_rating wines_rated
Cabernet Sauvignon 48.25220 88.61731 8840
Chardonnay 34.70414 88.32306 10868
Pinot Noir 47.87520 89.42654 12278
wine_table %>% 
  gt() %>% 
  tab_header(title = "Most Popular Wine Varieties",
             md("**Rating** *and* **Price**"))
Most Popular Wine Varieties
Rating and Price
variety mean_price mean_rating wines_rated
Cabernet Sauvignon 48.25220 88.61731 8840
Chardonnay 34.70414 88.32306 10868
Pinot Noir 47.87520 89.42654 12278
wine_table %>% 
  gt() %>% 
  tab_header(title = "Most Popular Wine Varieties",
             md("**Rating** *and* **Price**")) %>% 
  tab_source_note(source_note = 
                   md("*Data source: WineEnthusiast via #TidyTuesday*"))
Most Popular Wine Varieties
Rating and Price
variety mean_price mean_rating wines_rated
Cabernet Sauvignon 48.25220 88.61731 8840
Chardonnay 34.70414 88.32306 10868
Pinot Noir 47.87520 89.42654 12278
Data source: WineEnthusiast via #TidyTuesday
wine_table %>% 
  gt() %>% 
  tab_header(title = "Most Popular Wine Varieties",
             md("**Rating** *and* **Price**")) %>% 
  tab_source_note(source_note = 
                   md("*Data source: WineEnthusiast via #TidyTuesday*")) %>% 
  cols_label(variety = md("**Variety**"),
    mean_price = "Avg. Price",
    mean_rating = "Avg. Rating",
    wines_rated = "# Rated") 
Most Popular Wine Varieties
Rating and Price
Variety Avg. Price Avg. Rating # Rated
Cabernet Sauvignon 48.25220 88.61731 8840
Chardonnay 34.70414 88.32306 10868
Pinot Noir 47.87520 89.42654 12278
Data source: WineEnthusiast via #TidyTuesday
wine_table %>% 
  gt() %>% 
  tab_header(title = "Most Popular Wine Varieties",
             md("**Rating** *and* **Price**")) %>% 
  tab_source_note(source_note = 
                   md("*Data source: WineEnthusiast via #TidyTuesday*")) %>% 
  cols_label(variety = md("**Variety**"),
    mean_price = "Avg. Price",
    mean_rating = "Avg. Rating",
    wines_rated = "# Rated") %>% 
  fmt_currency(mean_price) %>% 
  fmt_number(mean_rating, decimals = 1) %>% 
  fmt_number(wines_rated, use_seps = TRUE, decimals = 0)
Most Popular Wine Varieties
Rating and Price
Variety Avg. Price Avg. Rating # Rated
Cabernet Sauvignon $48.25 88.6 8,840
Chardonnay $34.70 88.3 10,868
Pinot Noir $47.88 89.4 12,278
Data source: WineEnthusiast via #TidyTuesday

Example 2: mpg

mpg_gt <- mpg %>% 
  filter(manufacturer %in% c("audi","ford","hyundai","toyota","volkswagen")) %>% 
  group_by(manufacturer, class, year) %>% 
  summarize(hwy = mean(hwy),
            cty = mean(cty)) %>% 
  pivot_wider(names_from = year, values_from = c(hwy, cty)) %>% 
  na.omit() %>% 
  ungroup()

mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt()
manufacturer class hwy_1999 hwy_2008 cty_1999 cty_2008
toyota compact 29.42857 32.20000 21.71429 23.00000
toyota midsize 27.00000 30.00000 19.50000 20.33333
hyundai midsize 26.25000 29.66667 18.00000 20.33333
volkswagen compact 28.12500 29.00000 20.37500 21.33333
volkswagen subcompact 35.00000 28.50000 26.00000 20.00000
volkswagen midsize 27.50000 27.66667 18.25000 19.00000
audi compact 26.37500 27.57143 17.37500 18.57143
hyundai subcompact 27.50000 25.40000 19.00000 18.00000
audi midsize 24.00000 24.00000 15.00000 16.50000
ford subcompact 23.50000 23.00000 16.50000 15.40000
toyota pickup 19.00000 20.00000 15.25000 16.00000
ford suv 17.33333 18.66667 13.00000 12.66667
toyota suv 18.20000 18.33333 14.40000 14.33333
ford pickup 16.20000 17.00000 13.00000 13.00000
mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008))
manufacturer class 1999 2008
hwy_1999 cty_1999 hwy_2008 cty_2008
toyota compact 29.42857 21.71429 32.20000 23.00000
toyota midsize 27.00000 19.50000 30.00000 20.33333
hyundai midsize 26.25000 18.00000 29.66667 20.33333
volkswagen compact 28.12500 20.37500 29.00000 21.33333
volkswagen subcompact 35.00000 26.00000 28.50000 20.00000
volkswagen midsize 27.50000 18.25000 27.66667 19.00000
audi compact 26.37500 17.37500 27.57143 18.57143
hyundai subcompact 27.50000 19.00000 25.40000 18.00000
audi midsize 24.00000 15.00000 24.00000 16.50000
ford subcompact 23.50000 16.50000 23.00000 15.40000
toyota pickup 19.00000 15.25000 20.00000 16.00000
ford suv 17.33333 13.00000 18.66667 12.66667
toyota suv 18.20000 14.40000 18.33333 14.33333
ford pickup 16.20000 13.00000 17.00000 13.00000
mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG")
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
toyota compact 29.42857 21.71429 32.20000 23.00000
toyota midsize 27.00000 19.50000 30.00000 20.33333
hyundai midsize 26.25000 18.00000 29.66667 20.33333
volkswagen compact 28.12500 20.37500 29.00000 21.33333
volkswagen subcompact 35.00000 26.00000 28.50000 20.00000
volkswagen midsize 27.50000 18.25000 27.66667 19.00000
audi compact 26.37500 17.37500 27.57143 18.57143
hyundai subcompact 27.50000 19.00000 25.40000 18.00000
audi midsize 24.00000 15.00000 24.00000 16.50000
ford subcompact 23.50000 16.50000 23.00000 15.40000
toyota pickup 19.00000 15.25000 20.00000 16.00000
ford suv 17.33333 13.00000 18.66667 12.66667
toyota suv 18.20000 14.40000 18.33333 14.33333
ford pickup 16.20000 13.00000 17.00000 13.00000
mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG") %>% 
  fmt_number(contains("1999") | contains("2008"), decimals = 1)
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
toyota compact 29.4 21.7 32.2 23.0
toyota midsize 27.0 19.5 30.0 20.3
hyundai midsize 26.2 18.0 29.7 20.3
volkswagen compact 28.1 20.4 29.0 21.3
volkswagen subcompact 35.0 26.0 28.5 20.0
volkswagen midsize 27.5 18.2 27.7 19.0
audi compact 26.4 17.4 27.6 18.6
hyundai subcompact 27.5 19.0 25.4 18.0
audi midsize 24.0 15.0 24.0 16.5
ford subcompact 23.5 16.5 23.0 15.4
toyota pickup 19.0 15.2 20.0 16.0
ford suv 17.3 13.0 18.7 12.7
toyota suv 18.2 14.4 18.3 14.3
ford pickup 16.2 13.0 17.0 13.0
mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG") %>% 
  fmt_number(contains("1999") | contains("2008"), decimals = 1) %>% 
  tab_row_group(label = "Efficient!",
                rows = hwy_2008 >= 25) %>%
  tab_row_group(label = "Less efficient",
                rows = hwy_2008 < 25) %>% 
  row_group_order(groups = c("Efficient!", "Less efficient"))
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
Efficient!
toyota compact 29.4 21.7 32.2 23.0
toyota midsize 27.0 19.5 30.0 20.3
hyundai midsize 26.2 18.0 29.7 20.3
volkswagen compact 28.1 20.4 29.0 21.3
volkswagen subcompact 35.0 26.0 28.5 20.0
volkswagen midsize 27.5 18.2 27.7 19.0
audi compact 26.4 17.4 27.6 18.6
hyundai subcompact 27.5 19.0 25.4 18.0
Less efficient
audi midsize 24.0 15.0 24.0 16.5
ford subcompact 23.5 16.5 23.0 15.4
toyota pickup 19.0 15.2 20.0 16.0
ford suv 17.3 13.0 18.7 12.7
toyota suv 18.2 14.4 18.3 14.3
ford pickup 16.2 13.0 17.0 13.0
mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG") %>% 
  fmt_number(contains("1999") | contains("2008"), decimals = 1) %>% 
  tab_row_group(label = "Efficient!",
                rows = hwy_2008 >= 25) %>%
  tab_row_group(label = "Less efficient",
                rows = hwy_2008 < 25) %>% 
  row_group_order(groups = c("Efficient!", "Less efficient")) %>% 
  tab_footnote(footnote = "Defined as highway mpg >= 25 in 2008",
               locations = cells_row_groups(groups = "Efficient!"))
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
Efficient!1
toyota compact 29.4 21.7 32.2 23.0
toyota midsize 27.0 19.5 30.0 20.3
hyundai midsize 26.2 18.0 29.7 20.3
volkswagen compact 28.1 20.4 29.0 21.3
volkswagen subcompact 35.0 26.0 28.5 20.0
volkswagen midsize 27.5 18.2 27.7 19.0
audi compact 26.4 17.4 27.6 18.6
hyundai subcompact 27.5 19.0 25.4 18.0
Less efficient
audi midsize 24.0 15.0 24.0 16.5
ford subcompact 23.5 16.5 23.0 15.4
toyota pickup 19.0 15.2 20.0 16.0
ford suv 17.3 13.0 18.7 12.7
toyota suv 18.2 14.4 18.3 14.3
ford pickup 16.2 13.0 17.0 13.0

1 Defined as highway mpg >= 25 in 2008

mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG") %>% 
  fmt_number(contains("1999") | contains("2008"), decimals = 1) %>% 
  tab_row_group(label = "Efficient!",
                rows = hwy_2008 >= 25) %>%
  tab_row_group(label = "Less efficient",
                rows = hwy_2008 < 25) %>% 
  row_group_order(groups = c("Efficient!", "Less efficient")) %>% 
  tab_footnote(footnote = "Defined as highway mpg >= 25 in 2008",
               locations = cells_row_groups(groups = "Efficient!")) %>% 
  data_color(columns = hwy_2008,
             colors = scales::col_numeric(paletteer::paletteer_d(
               palette = "ggsci::red_material") %>% as.character(),
               domain = NULL))
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
Efficient!1
toyota compact 29.4 21.7 32.2 23.0
toyota midsize 27.0 19.5 30.0 20.3
hyundai midsize 26.2 18.0 29.7 20.3
volkswagen compact 28.1 20.4 29.0 21.3
volkswagen subcompact 35.0 26.0 28.5 20.0
volkswagen midsize 27.5 18.2 27.7 19.0
audi compact 26.4 17.4 27.6 18.6
hyundai subcompact 27.5 19.0 25.4 18.0
Less efficient
audi midsize 24.0 15.0 24.0 16.5
ford subcompact 23.5 16.5 23.0 15.4
toyota pickup 19.0 15.2 20.0 16.0
ford suv 17.3 13.0 18.7 12.7
toyota suv 18.2 14.4 18.3 14.3
ford pickup 16.2 13.0 17.0 13.0

1 Defined as highway mpg >= 25 in 2008

mpg_gt %>% 
  arrange(desc(hwy_2008)) %>% 
  gt() %>% 
  tab_spanner(label = md("**1999**"),
    columns = c(hwy_1999, cty_1999)) %>%
  tab_spanner(label = md("**2008**"),
    columns = c(hwy_2008, cty_2008)) %>% 
  cols_label(manufacturer = md("*Manufacturer*"),
             class = md("*Class*"),
             hwy_1999 = "Hwy MPG",
             hwy_2008 = "Hwy MPG",
             cty_1999 = "City MPG",
             cty_2008 = "City MPG") %>% 
  fmt_number(contains("1999") | contains("2008"), decimals = 1) %>% 
  tab_row_group(label = "Efficient!",
                rows = hwy_2008 >= 25) %>%
  tab_row_group(label = "Less efficient",
                rows = hwy_2008 < 25) %>% 
  row_group_order(groups = c("Efficient!", "Less efficient")) %>% 
  tab_footnote(footnote = "Defined as highway mpg >= 25 in 2008",
               locations = cells_row_groups(groups = "Efficient!")) %>% 
  data_color(columns = hwy_2008,
             colors = scales::col_numeric(paletteer::paletteer_d(
               palette = "ggsci::red_material") %>% as.character(),
               domain = NULL)) %>% 
  tab_options(heading.subtitle.font.size = 12,
              heading.align = "left",
              table.border.top.color = "black",
              table.border.bottom.color = "black",
              column_labels.border.top.color = "black",
              column_labels.border.bottom.color = "black",
              column_labels.border.bottom.width = px(2))
Manufacturer Class 1999 2008
Hwy MPG City MPG Hwy MPG City MPG
Efficient!1
toyota compact 29.4 21.7 32.2 23.0
toyota midsize 27.0 19.5 30.0 20.3
hyundai midsize 26.2 18.0 29.7 20.3
volkswagen compact 28.1 20.4 29.0 21.3
volkswagen subcompact 35.0 26.0 28.5 20.0
volkswagen midsize 27.5 18.2 27.7 19.0
audi compact 26.4 17.4 27.6 18.6
hyundai subcompact 27.5 19.0 25.4 18.0
Less efficient
audi midsize 24.0 15.0 24.0 16.5
ford subcompact 23.5 16.5 23.0 15.4
toyota pickup 19.0 15.2 20.0 16.0
ford suv 17.3 13.0 18.7 12.7
toyota suv 18.2 14.4 18.3 14.3
ford pickup 16.2 13.0 17.0 13.0

1 Defined as highway mpg >= 25 in 2008

gtsummary

tbl_regression(linear_fit)
Characteristic Beta 95% CI1 p-value
sex
female
male 375 258, 492 <0.001
species
Adelie
Chinstrap -272 -475, -70 0.009
Gentoo 1,000 676, 1,324 <0.001
flipper_length_mm 16 8.6, 24 <0.001
bill_length_mm 19 0.37, 37 0.046
bill_depth_mm 69 21, 117 0.005

1 CI = Confidence Interval

tbl_regression(linear_fit,
               label = list(flipper_length_mm = "Flipper Length (mm)",
                            bill_length_mm = "Bill Length (mm)",
                            bill_depth_mm = "Bill Depth (mm)"))
Characteristic Beta 95% CI1 p-value
sex
female
male 375 258, 492 <0.001
species
Adelie
Chinstrap -272 -475, -70 0.009
Gentoo 1,000 676, 1,324 <0.001
Flipper Length (mm) 16 8.6, 24 <0.001
Bill Length (mm) 19 0.37, 37 0.046
Bill Depth (mm) 69 21, 117 0.005

1 CI = Confidence Interval

tbl_regression(linear_fit,
               label = list(flipper_length_mm = "Flipper Length (mm)",
                            bill_length_mm = "Bill Length (mm)",
                            bill_depth_mm = "Bill Depth (mm)")) %>% 
  bold_labels()
Characteristic Beta 95% CI1 p-value
sex
female
male 375 258, 492 <0.001
species
Adelie
Chinstrap -272 -475, -70 0.009
Gentoo 1,000 676, 1,324 <0.001
Flipper Length (mm) 16 8.6, 24 <0.001
Bill Length (mm) 19 0.37, 37 0.046
Bill Depth (mm) 69 21, 117 0.005

1 CI = Confidence Interval

gt_theme <- function(data) {
  tab_options(data = data,
              heading.subtitle.font.size = 12,
              heading.align = "left",
              table.border.top.color = "black",
              table.border.bottom.color = "black",
              column_labels.border.top.color = "black",
              column_labels.border.bottom.color = "black",
              column_labels.border.bottom.width = px(2))
}

tbl_regression(linear_fit,
               label = list(flipper_length_mm = "Flipper Length (mm)",
                            bill_length_mm = "Bill Length (mm)",
                            bill_depth_mm = "Bill Depth (mm)")) %>% 
  bold_labels() %>% 
  as_gt() %>%
  gt_theme()
Characteristic Beta 95% CI1 p-value
sex
female
male 375 258, 492 <0.001
species
Adelie
Chinstrap -272 -475, -70 0.009
Gentoo 1,000 676, 1,324 <0.001
Flipper Length (mm) 16 8.6, 24 <0.001
Bill Length (mm) 19 0.37, 37 0.046
Bill Depth (mm) 69 21, 117 0.005

1 CI = Confidence Interval