Created
January 17, 2018 17:27
-
-
Save walkerke/b7f6ee1a37247077a6a43f98434d8d2b 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(tidyverse) | |
library(animation) | |
library(readxl) | |
library(extrafont) | |
# Download file from: https://esa.un.org/unpd/wup/cd-rom/WUP2014_XLS_CD_FILES/WUP2014-F11a-30_Largest_Cities.xls | |
df <- read_excel('WUP2014-F11a-30_Largest_Cities.xls', skip = 17, | |
col_names = c("index", "year", "order", "ccode", | |
"country", "city", "name", "note", "lat", | |
"lon", "pop")) %>% | |
mutate(name = str_replace(name, " \\(.*|,.*", "")) %>% | |
mutate(name = str_trim(name)) | |
na <- c("New York-Newark", "Chicago", "Los Angeles-Long Beach-Santa Ana", | |
"Philadelphia", "Detroit", "Boston", "San Francisco-Oakland") | |
latam <- c("Buenos Aires", "Ciudad de México", "Rio de Janeiro", | |
"São Paulo", "Lima") | |
europe <- c("London", "Paris", "Moskva", "Berlin", "Sankt Peterburg", | |
"Birmingham", "Roma", "Milano", "Barcelona", | |
"Istanbul", "Manchester", "Madrid") | |
sasia <- c("Kolkata", "Mumbai", "Delhi", "Tehran", "Karachi", | |
"Dhaka", "Chennai", "Bangalore", "Lahore", | |
"Hyderabad") | |
easia <- c("Tokyo", "Kinki M.M.A.", "Shanghai", "Tianjin", "Chukyo M.M.A.", | |
"Shenyang", "Beijing", "Hong Kong", "Kitakyushu-Fukuoka M.M.A.", | |
"Jakarta", "Seoul", "Manila", "Krung Thep", "Chongqing", | |
"Guangzhou", "Shenzhen") | |
africa <- c("Al-Qahirah", "Lagos", "Kinshasa") | |
dfb <- df %>% | |
mutate(region = case_when( | |
name %in% na ~ "United States", | |
name %in% latam ~ "Latin America", | |
name %in% europe ~ "Europe", | |
name %in% sasia ~ "South Asia", | |
name %in% easia ~ "East Asia", | |
name %in% africa ~ "Africa" | |
), | |
poplabel = paste0(round(pop, 1), "m") | |
) | |
saveGIF({ | |
for (i in seq(1950, 2030, 5)) { | |
yearly <- filter(dfb, year == i) | |
g <- ggplot() + | |
geom_bar(data = yearly, aes(y = pop, x = reorder(name, pop), | |
fill = region), stat = "identity") + | |
geom_text(data = yearly, aes(y = pop + 1.5, x = reorder(name, pop), | |
label = poplabel), fontface = "bold") + | |
coord_flip() + | |
scale_y_continuous(limits = c(0, 42), expand = c(0, 0)) + | |
scale_fill_brewer(palette = "Set1") + | |
theme_minimal(base_size = 16, base_family = "Tahoma") + | |
theme(panel.grid.major.x = element_blank(), | |
panel.grid.minor.x = element_blank(), | |
panel.grid.major.y = element_blank(), | |
panel.grid.minor.y = element_blank(), | |
plot.title = element_text(face = "bold"), | |
axis.text.x = element_blank(), | |
legend.position = "bottom") + | |
labs(y = "", | |
x = "", | |
fill = "", | |
caption = "Data source: United Nations World Urbanization Prospects | Chart by @kyle_e_walker", | |
title = paste0("30 largest metropolitan areas, ", as.character(round(i, 0)))) | |
print(g) | |
} | |
}, movie.name = "pop.gif", interval = 1.5, ani.width = 700, ani.height = 600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment