Created
June 4, 2016 10:47
-
-
Save walkerke/909068529346077354791d4f2e7f9ca2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(idbr) | |
library(ggplot2) | |
library(dplyr) | |
library(gganimate) | |
library(animation) | |
idb_api_key("Your key goes here") | |
male <- idb1('NI', 1990:2050, sex = 'male') %>% | |
mutate(POP = POP * -1, | |
SEX = 'Male') | |
female <- idb1('NI', 1990:2050, sex = 'female') %>% | |
mutate(SEX = 'Female') | |
nigeria <- rbind(male, female) | |
# Animate it with gganimate | |
g1 <- ggplot(nigeria, aes(x = AGE, y = POP, fill = SEX, width = 1, frame = time)) + | |
coord_fixed() + | |
coord_flip() + | |
geom_bar(data = subset(nigeria, SEX == "Female"), stat = "identity", position = 'identity') + | |
geom_bar(data = subset(nigeria, SEX == "Male"), stat = "identity", position = 'identity') + | |
scale_y_continuous(breaks = seq(-5000000, 5000000, 2500000), | |
labels = c('5m', '2.5m', '0', '2.5m', '5m'), | |
limits = c(min(nigeria$POP), max(nigeria$POP))) + | |
theme_minimal(base_size = 14, base_family = "Tahoma") + | |
scale_fill_manual(values = c('#98df8a', '#2ca02c')) + | |
ggtitle('Population structure of Nigeria,') + | |
ylab('Population') + | |
xlab('Age') + | |
theme(legend.position = "bottom", legend.title = element_blank()) + | |
labs(caption = 'Chart by @kyle_e_walker | Data source: US Census Bureau IDB via the idbr R package') + | |
guides(fill = guide_legend(reverse = TRUE)) | |
gg_animate(g1, interval = 0.1, ani.width = 700, ani.height = 600) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment