library(tidyverse)
library(gt)
gapminder_data <- gapminder::gapminder |>
janitor::clean_names() |>
select(continent, country, year, life_exp) |>
mutate(
year = as.character(year),
# Year is really categorical with numeric labels
country = as.character(country)
)
gapminder_data
## # A tibble: 1,704 × 4
## continent country year life_exp
## <fct> <chr> <chr> <dbl>
## 1 Asia Afghanistan 1952 28.8
## 2 Asia Afghanistan 1957 30.3
## 3 Asia Afghanistan 1962 32.0
## 4 Asia Afghanistan 1967 34.0
## 5 Asia Afghanistan 1972 36.1
## 6 Asia Afghanistan 1977 38.4
## 7 Asia Afghanistan 1982 39.9
## 8 Asia Afghanistan 1987 40.8
## 9 Asia Afghanistan 1992 41.7
## 10 Asia Afghanistan 1997 41.8
## # ℹ 1,694 more rows2 Fancy stuff / Eye catchers
In this chapter, we’re going to learn how to add fancy elements like plots, icon and images to {gt} tables. We’re going to start this chapter by using a selection of the gapminder data set from {gapminder}.
Let’s bring this into a table using some fancy elements. Many such elements can be added relatively easily with {gtExtras}. For example, here’s a summary table of our data set.
library(gtExtras)
gt_plt_summary(gapminder_data)
## Warning in geom_point(data = NULL, aes(x = rng_vals[1], y = 1), color = "transparent", : All aesthetics have length 1, but the data has 1704 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_point(data = NULL, aes(x = rng_vals[2], y = 1), color = "transparent", : All aesthetics have length 1, but the data has 1704 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.| gapminder_data | ||||||
| 1704 rows x 4 cols | ||||||
| Column | Plot Overview | Missing | Mean | Median | SD | |
|---|---|---|---|---|---|---|
| continent | 0.0% | — | — | — | ||
| country | 0.0% | — | — | — | ||
| year | 0.0% | — | — | — | ||
| life_exp | 0.0% | 59.5 | 60.7 | 12.9 | ||
As you can see, this table includes icons in the first column (categorical or continuous variables) and a plot overview in the third column. Automatic tables like this can give you a feeling for the data at a glance. For example, we can see that there are 12 years and 142 countries present in the data set. Also, no values are missing.
Since we have quite a lot of info on many countries and years, let us make our data set a bit smaller. We don’t want to create huge tables (yet). Just like in the last chapter, we will have to reorder our data a bit so that it’s already in a good table format.
selected_countries <- gapminder_data |>
# Filter to use only six years (those that end in 7)
filter(str_ends(year, "7")) |>
# sample two countries per continent
group_by(continent, country) |>
nest() |>
group_by(continent) |>
slice_sample(n = 2) |>
ungroup() |>
unnest(data) |>
# Rearrange the data into table format
pivot_wider(names_from = year, names_prefix = 'year', values_from = life_exp)
selected_countries
## # A tibble: 10 × 8
## continent country year1957 year1967 year1977 year1987 year1997 year2007
## <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Africa Egypt 44.4 49.3 53.3 59.8 67.2 71.3
## 2 Africa Sierra Leone 31.6 34.1 36.8 40.0 39.9 42.6
## 3 Americas Nicaragua 45.4 51.9 57.5 62.0 68.4 72.9
## 4 Americas Jamaica 62.6 67.5 70.1 71.8 72.3 72.6
## 5 Asia Syria 48.3 53.7 61.2 67.0 71.5 74.1
## 6 Asia Singapore 63.2 67.9 70.8 73.6 77.2 80.0
## 7 Europe Netherlands 73.0 73.8 75.2 76.8 78.0 79.8
## 8 Europe United Kingd… 70.4 71.4 72.8 75.0 77.2 79.4
## 9 Oceania New Zealand 70.3 71.5 72.2 74.3 77.6 80.2
## 10 Oceania Australia 70.3 71.1 73.5 76.3 78.8 81.2From this we can create a {gt} table just like we learned in the last chapter. And with {gtExtras} we can apply a cool FiveThirtyEight theme to our table.
# New column names
new_colnames <- colnames(selected_countries) |> str_remove('(country|year)')
names(new_colnames) <- colnames(selected_countries)
selected_countries |>
gt(groupname_col = 'continent') |>
tab_header(
title = 'Life Expectancies over time',
subtitle = 'Data is courtesy of the Gapminder foundation'
) |>
cols_label(.list = new_colnames) |>
fmt_number(columns = where(is.numeric), decimals = 2) |>
gt_theme_538()