Skip to content

Instantly share code, notes, and snippets.

@oliviergimenez
Created April 27, 2024 20:46
Show Gist options
  • Save oliviergimenez/d332212b0e21c5fa076802f59614c777 to your computer and use it in GitHub Desktop.
Save oliviergimenez/d332212b0e21c5fa076802f59614c777 to your computer and use it in GitHub Desktop.
# libraries needed
library(tidyverse)
library(viridis)
library(gganimate)
library(wbstats)
# rosling chart in one command
# pull the country data down from the World Bank - three indicators
wbstats::wb(indicator = c("SP.DYN.LE00.IN", "NY.GDP.PCAP.CD", "SP.POP.TOTL"),
country = "countries_only", startdate = 1960, enddate = 2018) %>%
# pull down mapping of countries to regions and join
dplyr::left_join(wbstats::wbcountries() %>%
dplyr::select(iso3c, region)) %>%
# spread the three indicators
tidyr::pivot_wider(id_cols = c("date", "country", "region"), names_from = indicator, values_from = value) %>%
# plot the data
ggplot2::ggplot(aes(x = log(`GDP per capita (current US$)`), y = `Life expectancy at birth, total (years)`,
size = `Population, total`)) +
ggplot2::geom_point(alpha = 0.5, aes(color = region)) +
ggplot2::scale_size(range = c(.1, 16), guide = FALSE) +
ggplot2::scale_x_continuous(limits = c(2.5, 12.5)) +
ggplot2::scale_y_continuous(limits = c(30, 90)) +
viridis::scale_color_viridis(discrete = TRUE, name = "Region", option = "viridis") +
ggplot2::labs(x = "Log GDP per capita",
y = "Life expectancy at birth") +
ggplot2::theme_classic() +
ggplot2::geom_text(aes(x = 7.5, y = 60, label = date), size = 14, color = 'lightgrey', family = 'Oswald') +
# animate it over years
gganimate::transition_states(date, transition_length = 1, state_length = 1) +
gganimate::ease_aes('cubic-in-out')
@oliviergimenez
Copy link
Author

file16d04c65a01a

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