library(tidyverse)
library(gt)
<- gapminder::gapminder |>
gapminder_data ::clean_names() |>
janitorselect(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 rows
2 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.
<- gapminder_data |>
selected_countries # 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.2
From 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
<- colnames(selected_countries) |> str_remove('(country|year)')
new_colnames 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()
Life Expectancies over time | ||||||
Data is courtesy of the Gapminder foundation | ||||||
1957 | 1967 | 1977 | 1987 | 1997 | 2007 | |
---|---|---|---|---|---|---|
Africa | ||||||
Egypt | 44.44 | 49.29 | 53.32 | 59.80 | 67.22 | 71.34 |
Sierra Leone | 31.57 | 34.11 | 36.79 | 40.01 | 39.90 | 42.57 |
Americas | ||||||
Nicaragua | 45.43 | 51.88 | 57.47 | 62.01 | 68.43 | 72.90 |
Jamaica | 62.61 | 67.51 | 70.11 | 71.77 | 72.26 | 72.57 |
Asia | ||||||
Syria | 48.28 | 53.66 | 61.20 | 66.97 | 71.53 | 74.14 |
Singapore | 63.18 | 67.95 | 70.80 | 73.56 | 77.16 | 79.97 |
Europe | ||||||
Netherlands | 72.99 | 73.82 | 75.24 | 76.83 | 78.03 | 79.76 |
United Kingdom | 70.42 | 71.36 | 72.76 | 75.01 | 77.22 | 79.42 |
Oceania | ||||||
New Zealand | 70.26 | 71.52 | 72.22 | 74.32 | 77.55 | 80.20 |
Australia | 70.33 | 71.10 | 73.49 | 76.32 | 78.83 | 81.23 |
2.1 Transform columns into heatmaps
In this table, we can see that Sierra Leone had by far the lowest life expectancy in 2007 (among the depicted countries). We can figure this out by comparing the numbers in the most recent column one-by-one.
But that takes quite a lot of effort. Instead, let us make that easier to see by transforming that column into a heat map. To do so, just pass our table to gt_color_rows()
1. What you’ll need to specify, is
- the targeted columns
- the range of the values that are supposed to be colored
- two colors that are used in a linear gradient
# Two colors from the Okabe Ito color palette
<- c("#CC79A7", "#009E73")
color_palette
|>
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() |>
gt_color_rows(
columns = year2007,
domain = c(30, 85),
palette = color_palette
)## Warning: Since gt v0.9.0, the `colors` argument has been deprecated.
## • Please use the `fn` argument instead.
## This warning is displayed once every 8 hours.
Life Expectancies over time | ||||||
Data is courtesy of the Gapminder foundation | ||||||
1957 | 1967 | 1977 | 1987 | 1997 | 2007 | |
---|---|---|---|---|---|---|
Africa | ||||||
Egypt | 44.44 | 49.29 | 53.32 | 59.80 | 67.22 | 71.34 |
Sierra Leone | 31.57 | 34.11 | 36.79 | 40.01 | 39.90 | 42.57 |
Americas | ||||||
Nicaragua | 45.43 | 51.88 | 57.47 | 62.01 | 68.43 | 72.90 |
Jamaica | 62.61 | 67.51 | 70.11 | 71.77 | 72.26 | 72.57 |
Asia | ||||||
Syria | 48.28 | 53.66 | 61.20 | 66.97 | 71.53 | 74.14 |
Singapore | 63.18 | 67.95 | 70.80 | 73.56 | 77.16 | 79.97 |
Europe | ||||||
Netherlands | 72.99 | 73.82 | 75.24 | 76.83 | 78.03 | 79.76 |
United Kingdom | 70.42 | 71.36 | 72.76 | 75.01 | 77.22 | 79.42 |
Oceania | ||||||
New Zealand | 70.26 | 71.52 | 72.22 | 74.32 | 77.55 | 80.20 |
Australia | 70.33 | 71.10 | 73.49 | 76.32 | 78.83 | 81.23 |
We could also do this for more columns. For example, we could also do the same with the 1957 column.
# Two colors from the Okabe Ito color palette
<- c("#CC79A7", "#009E73")
color_palette
|>
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() |>
gt_color_rows(
columns = c(year1957, year2007),
domain = c(30, 85),
palette = color_palette
)