Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Last active September 4, 2018 13:05
Show Gist options
  • Save mschnetzer/3264b3997189867d5b0a9822ba569b2a to your computer and use it in GitHub Desktop.
Save mschnetzer/3264b3997189867d5b0a9822ba569b2a to your computer and use it in GitHub Desktop.
World Tile Grid of Quintiles of GDP per capita, 1995-2015 (https://twitter.com/matschnetzer/status/1036879668606107648)
library(worldtilegrid)
library(plyr)
library(tidyverse)
library(countrycode)
library(viridis)
library(msthemes)
# Get GDP per capita data from UNdata http://bit.ly/2N4hUlV
data <- read.csv("undata.csv",dec=".") %>% rename(Country=Country.or.Area) %>% select(-Value.Footnotes)
# Check compatibility for countrycode-transformation
data %>% filter(!Country %in% countrycode::codelist$country.name.en) %>%
select(Country)
# Harmonize country names manually
data <- data %>% mutate_at(vars(Country),funs(iconv(., "UTF-8", "WINDOWS-1252"))) %>%
mutate_at(vars(Country),funs(gsub(" and "," & ",.))) %>%
mutate_at(vars(Country), funs(revalue(., c("Côte d'Ivoire"="Côte d’Ivoire",
"Cabo Verde" = "Cape Verde",
"Congo" = "Congo - Brazzaville",
"Dem. Rep. Congo" = "Congo - Kinshasa",
"Czech Republic" = "Czechia",
"Hong Kong SAR, China" = "Hong Kong SAR China",
"Korea" = "South Korea",
"Kyrgyz Republic" = "Kyrgyzstan",
"Lao PDR" = "Laos",
"Macao SAR, China" = "Macau SAR China",
"Micronesia" = "Micronesia (Federated States of)",
"Myanmar" = "Myanmar (Burma)",
"São Tomé & Principe" = "São Tomé & Príncipe",
"Slovak Republic" = "Slovakia",
"St. Vincent & the Grenadines" = "St. Vincent & Grenadines",
"The Bahamas" = "Bahamas",
"The Gambia" = "Gambia"))))
# Keep only countries with data for both years or all countries (line 38)
data <- data %>% filter(Country %in% countrycode::codelist$country.name.en) %>%
group_by(Country) %>% filter(n()>1) %>% ungroup() %>%
group_by(Year) %>% mutate(quintile=as.factor(ntile(Value,5))) %>%
mutate_at(vars(quintile),funs(revalue(.,c("1"="Poorest Quintile",
"2"="2nd Quintile",
"3"="3rd Quintile",
"4"="4th Quintile",
"5"="Richest Quintile"))))
data$ctycode <- countrycode(data$Country,"country.name","iso3c")
png("wtg_gdppc.png", width = 9, height = 6, units = 'in', res = 300)
ggplot(data, aes(country = ctycode, fill = quintile)) +
geom_wtg() +
geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label
coord_equal() +
facet_wrap(~Year) +
scale_fill_manual(values=msl_palette,na.value=alpha("#c3c3c3",0.3), name="") +
theme_ms(grid=F,dark=T,alttf=T) +
theme(axis.text = element_blank(),legend.position = "bottom",plot.margin=margin(10,10,10,10)) +
labs(title="Which countries climbed the income ladder?",subtitle="Quintiles of GDP per capita (PPP)",caption="Data: UN, World Bank. Figure: @matschnetzer")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment