Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created July 21, 2022 20:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/7b7e7680a005b180930a396af2e5fa15 to your computer and use it in GitHub Desktop.
Save andrewheiss/7b7e7680a005b180930a396af2e5fa15 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gapminder)
library(geomtextpath)
library(MetBrewer)

gapminder_lifeexp <- gapminder %>% 
  group_by(continent, year) %>% 
  summarize(avg_lifeexp = mean(lifeExp))

continent_order <- gapminder_lifeexp %>% 
  filter(year == 1952) %>% 
  arrange(desc(avg_lifeexp)) %>% 
  pull(continent) %>% 
  as.character()
# Assistant font from Google
# https://fonts.google.com/specimen/Assistant
gapminder_lifeexp %>% 
  mutate(continent = factor(continent, levels = continent_order, ordered = TRUE)) %>% 
  ggplot(aes(x = year, y = avg_lifeexp, color = continent)) +
  geom_textline(aes(label = continent, hjust = continent),
                linewidth = 1, size = 4,
                family = "Assistant SemiBold") +
  scale_hjust_discrete(range = c(0.05, 0.25)) +
  scale_color_manual(values = met.brewer("Austria", 5)) +
  guides(color = "none") +
  labs(x = NULL, y = "Average life expectancy", 
       title = "Average life expectancy over time",
       caption = "Source: The Gapminder Project") +
  theme_minimal(base_family = "Assistant") +
  theme(panel.grid.minor = element_blank(),
        plot.title = element_text(family = "Assistant", face = "bold", size = rel(1.5)),
        plot.caption = element_text(family = "Assistant ExtraLight", size = rel(0.7), hjust = 0,
                                    margin = margin(t = 15)))

Created on 2022-07-21 by the reprex package (v2.0.1)

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