|
setwd("~/Documents/Data/LIS") |
|
|
|
## list of countries |
|
|
|
lis = read.csv("lis.tsv", sep = "\t", header = FALSE) |
|
lis$ctn = countrycode(lis$V1, "country.name", "continent") |
|
len = sapply(lis$V1, function(x) max(lis$V3[lis$V1 == x]) - min(lis$V2[lis$V1 == x])) |
|
qplot(data = lis, y = reorder(V1, len, mean), yend = V1, x = V2, xend = V3, |
|
size = I(2), geom = "segment") + |
|
theme_minimal(16) + |
|
labs(y = NULL, x = NULL) |
|
|
|
lis = toupper(lis[, 1]) |
|
# lis = c("AUSTRIA", "BELGIUM", "BRAZIL", "CANADA", "CHINA", "COLOMBIA", |
|
# "CZECH REPUBLIC", "DENMARK", "ESTONIA", "FINLAND", "FRANCE", |
|
# "GERMANY", "GREECE", "GUATEMALA", "HUNGARY", "INDIA", "IRELAND", |
|
# "ISRAEL", "ITALY", "JAPAN", "LUXEMBOURG", "MEXICO", "NETHERLANDS", |
|
# "NORWAY", "PERU", "POLAND", "ROMANIA", "RUSSIA", "SLOVAK REPUBLIC", |
|
# "SLOVENIA", "SOUTH AFRICA", "SOUTH KOREA", "SPAIN", "SWEDEN", |
|
# "SWITZERLAND", "TAIWAN", "UNITED KINGDOM", "UNITED STATES", "URUGUAY") |
|
|
|
## ask for the data |
|
|
|
library(Quandl) |
|
library(plyr) |
|
library(reshape) |
|
|
|
lis = lapply(lis, function(country) { |
|
q = Quandl(paste0("LIS/LIS_", gsub("\\s", "", country)), collapse = "annual", |
|
authcode = "[TOKEN]") |
|
q = melt(q, id = "Year") |
|
year = as.numeric(substr(q$Year, 0, 4)) |
|
q = cbind(country, year, q) |
|
q$Year = NULL |
|
return(q) |
|
}) |
|
lis = rbind.fill(lis) |
|
sort_df(lis, c("cty", "year")) |
|
str(lis) |
|
|
|
if(!file.exists("lis.rda")) save(lis, file = "lis.rda") |
|
load("lis.rda") |
|
|
|
## plot |
|
|
|
library(countrycode) |
|
library(ggplot2) |
|
library(RColorBrewer) |
|
|
|
eur = (countrycode(lis$country, "country.name", "continent") == "Europe") |
|
usa = (lis$country == "UNITED STATES") |
|
var = "Percentile Ratio (90/10)" |
|
LIS = subset(lis[eur | usa, ], variable == var) |
|
|
|
(mu = mean(LIS$value, na.rm = TRUE)) |
|
(sd = sd(LIS$value, na.rm = TRUE)) |
|
|
|
simpleCap <- function(x) { |
|
s <- strsplit(x, " ")[[1]] |
|
paste(toupper(substring(s, 1,1)), substring(s, 2), |
|
sep="", collapse=" ") |
|
} |
|
LIS$country = sapply(as.character(tolower(LIS$country)), simpleCap) |
|
|
|
qplot(data = LIS, x = year, y = value, group = country, |
|
colour = factor(as.integer(value)), size = I(1), geom = "line") + |
|
geom_hline(y = mu, linetype = "dashed", colour = "grey") + |
|
geom_hline(y = mu + sd, linetype = "dotted", colour = "grey") + |
|
geom_hline(y = mu - sd, linetype = "dotted", colour = "grey") + |
|
scale_colour_manual("", values = rev(brewer.pal(5, "RdYlGn"))) + |
|
facet_wrap(~ country) + |
|
theme_minimal(16) + |
|
theme(axis.line.y = element_line(size = 1, colour = "black"), |
|
panel.grid = element_blank(), |
|
legend.position = "none") + |
|
labs(x = NULL, y = paste0(var, "\n")) |