Skip to content

Instantly share code, notes, and snippets.

@walkerke
Last active March 2, 2016 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save walkerke/5f7faa8ae10c5991b352 to your computer and use it in GitHub Desktop.
Save walkerke/5f7faa8ae10c5991b352 to your computer and use it in GitHub Desktop.
library(idbr)
library(ggplot2)
library(gganimate)
library(tweenr)
library(countrycode)
library(dplyr)
idb_api_key("Your API key goes here")
africa_fips <- countrycode(c('Nigeria', 'Uganda', 'Tanzania', 'Ghana'),
origin = 'country.name', destination = 'fips104')
df <- idb5(country = africa_fips, year = 1980:2016, variables = c('TFR', 'IMR'), country_name = TRUE)
df <- df %>%
select(-FIPS) %>%
mutate(ease = 'linear')
dt <- tween_elements(df, time = 'time', group = 'NAME', ease = 'ease', nframe = 100)
p <- ggplot(data = dt) +
geom_point(aes(x = IMR, y = TFR, color = .group, frame = time),
size = 8) +
geom_text(aes(x = IMR + 2, y = TFR + 0.2, color = .group, label = .group, frame = time)) +
geom_text(aes(x = 105, y = 3.5, frame = time,
label = round(time, 0)), color = 'black', size = 14) +
guides(color = FALSE) +
theme_grey(base_size = 14) +
xlab('Infant mortality rate') +
ylab('Total fertility rate')
animation::ani.options(interval = 0.000001)
gg_animate(p, title_frame = FALSE, filename = 'imr_tfr.gif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment