Skip to content

Instantly share code, notes, and snippets.

@cengel
Created May 31, 2019 01:13
Show Gist options
  • Save cengel/0cdad936f8fef866675b2d3c887c785d to your computer and use it in GitHub Desktop.
Save cengel/0cdad936f8fef866675b2d3c887c785d to your computer and use it in GitHub Desktop.
# Original Idea and code by Jules Beley
# https://github.com/julesbeley/Erasmusmap/blob/master/Erasmus.R
# https://medium.com/@jules.beley/making-a-map-with-eu-data-on-r-erasmus-exchanges-by-country-3f5734dcd4ff
library(tidyverse)
library(maps)
library(countrycode)
download.file("http://data.europa.eu/euodp/data/uploads/EAC/SM_2012_13_20141103_01.csv", "erasmus.csv") # this may take a little while
erasmus <- read.csv("erasmus.csv", sep = ";")
# create counts of home institutions
home <- erasmus %>%
mutate(HOME_INSTITUTION_CTRY_CDE = ifelse(str_detect(HOME_INSTITUTION_CTRY_CDE, "BE"), "BE", as.character(HOME_INSTITUTION_CTRY_CDE))) %>% # combine Belgium
count(HOME_INSTITUTION_CTRY_CDE)
# create counts of host institutions
host <- erasmus %>%
filter(HOST_INSTITUTION_COUNTRY_CDE != "???") %>% # remove unknowns
mutate(HOST_INSTITUTION_COUNTRY_CDE = ifelse(str_detect(HOST_INSTITUTION_COUNTRY_CDE, "BE"), "BE", as.character(HOST_INSTITUTION_COUNTRY_CDE))) %>% # combine Belgium
count(HOST_INSTITUTION_COUNTRY_CDE)
# fix the country code
countries <- countrycode(host$HOST_INSTITUTION_COUNTRY_CDE, "eurostat", "eurostat.name", custom_match = c(GR = 'Greece', DE = 'Germany', UK = 'UK'))
map_data("world") %>% # get world data
left_join(data.frame(country = countries, ratio = host$n/home$n), by = c("region" = "country")) %>% # join with the host/home ratio
ggplot(aes(x = long, y = lat, group = group, fill = ratio)) +
geom_polygon() +
coord_fixed(xlim = c(-9, 42.5), ylim = c(36, 70.1), ratio = 1.5) +
scale_fill_viridis_c(option = "plasma", direction = -1) +
theme_void() +
theme(panel.background = element_rect(fill = "lightcyan1"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment