Skip to content

Instantly share code, notes, and snippets.

@halhen halhen/GTI.r
Last active Jul 17, 2018

Embed
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.