Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing alpha.2 and alpha.3 #2

Open
mschnetzer opened this issue Sep 4, 2018 · 3 comments
Open

Confusing alpha.2 and alpha.3 #2

mschnetzer opened this issue Sep 4, 2018 · 3 comments
Assignees

Comments

@mschnetzer
Copy link

Hi Bob,

first, thanks a lot for this beautiful package! I am not sure whether I am not using it correctly or whether this is a small bug.

When I use three-digit country codes in my data frame and label the data with stat(alpha.3), R returns an error. It works when using stat(alpha.2) and the output yields Using alpha.3 while the labels are only two-digit. Conversely, when my data frame contains two-digit country codes, it only works when using stat(alpha.3). Then, R returns Using alpha.2 and plots three-digit labels.

This is the code I'm running: https://gist.github.com/mschnetzer/3264b3997189867d5b0a9822ba569b2a

Any ideas? Thanks,
Matthias

@hrbrmstr hrbrmstr self-assigned this Sep 4, 2018
@hrbrmstr
Copy link
Owner

hrbrmstr commented Sep 4, 2018

Putting this here for safe-keeping & context

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()

@hrbrmstr
Copy link
Owner

hrbrmstr commented Sep 4, 2018

Confirming this is the proper view for the UN Data:

image

@mschnetzer
Copy link
Author

mschnetzer commented Sep 4, 2018

Confirming that I downloaded the data exactly in this format! Thanks for looking into this issue...

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

No branches or pull requests

2 participants