Create a gist now

Instantly share code, notes, and snippets.

@halhen /GTI.r
Last active May 16, 2017

What would you like to do?
library(htmltab)
library(tidyverse)
library(stringr)
URL <- 'https://en.wikipedia.org/w/index.php?title=Global_Terrorism_Index&oldid=779504863'
df <- htmltab(URL, 5) %>%
transmute(Country = str_trim(Country),
score = as.numeric(Score))
URL.pop <- 'https://en.wikipedia.org/w/index.php?title=List_of_sovereign_states_and_dependent_territories_by_immigrant_population&oldid=780121365'
df.pop <- htmltab(URL.pop, 2, rm_nodata_cols=F) %>%
mutate(Country = str_trim(Country)) %>%
transmute(Country, p.immigrant = as.numeric(`Immigrants >> Of national (%)`))
df.continent <- read_csv('https://commondatastorage.googleapis.com/ckannet-storage/2012-07-26T090250/Countries-Continents-csv.csv')
df %>%
inner_join(df.pop, by='Country') %>%
inner_join(df.continent, by='Country') %>%
group_by(Continent) %>%
arrange(-score) %>%
mutate(rank = row_number()) %>%
{
ggplot(., aes(p.immigrant/100, score, color=Continent)) +
ggrepel::geom_label_repel(data=filter(., rank <= 3), aes(label=Country)) +
geom_point(size=3) +
#geom_smooth(se=FALSE, method='lm') +
scale_x_continuous(labels=scales::percent, limits=c(0, 0.4)) +
facet_wrap(~ Continent) +
labs(x="", y="", subtitle="Immigration and Global Terrorism Index", caption='Sources: Wikipedia <http://bit.ly/2pPtl1J> <http://bit.ly/2qLWGOG>') +
theme_henrik(grid='XY') +
theme(legend.position='none')
}
ggsave('/tmp/terror.svg', width=12, height=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment