Skip to content

Instantly share code, notes, and snippets.

@ismaeltocruz
Last active May 2, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaeltocruz/4e869af6f5888773c2341fbe56bd1556 to your computer and use it in GitHub Desktop.
Save ismaeltocruz/4e869af6f5888773c2341fbe56bd1556 to your computer and use it in GitHub Desktop.
library(openxlsx)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(maps)
gtd = read.xlsx('globalterrorismdb_0615dist.xlsx')
# Attacks per year
length(gtd$eventid) / length(unique(gtd$iyear))
# Deaths per year
sum(gtd$nkill, na.rm = T) / length(unique(gtd$iyear))
# Attacks by year
attacks_per_year = gtd %>% group_by(iyear) %>% summarise(Attacks = n())
ggplot(data = attacks_per_year, aes(x = iyear, y = Attacks, fill = iyear)) +
geom_bar(stat = 'identity') + ggtitle('Terrorist Attacks from 1970 to 2014') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
# Attacks by type
attack_type = gtd %>% group_by(attacktype1_txt) %>% summarise(Attacks = n())
attack_type$Percent = round((attack_type$Attacks/ sum(attack_type$Attacks)*100),2)
attack_type$Total = sum(attack_type$Attacks)
ggplot(data = attack_type, aes(x = 'Attack Types', y = Percent, fill = attacktype1_txt)) +
geom_bar(stat = 'identity', width = 1, position = 'dodge') +
ggtitle('Terrorist Attacks by Type') + xlab('') +
scale_fill_brewer(palette = 'RdBu', name = '') + theme_solarized(light = F)
# Attacks by type per year (Armed Assault, Bombing/Explosion, Assassination)
attack_type_year2 = gtd %>%
filter(attacktype1_txt == c('Armed Assault', 'Assassination', 'Bombing/Explosion')) %>%
group_by(iyear,attacktype1_txt) %>%
summarise(Attacks = n())
ggplot(data = attack_type_year2, aes(x = iyear, y = Attacks,
fill = attacktype1_txt)) + geom_bar(stat = 'identity', position = 'dodge') +
ggtitle('Attack Type by Year') + xlab('Year') +
scale_fill_brewer(palette = 'RdBu', name = '') + theme_solarized(light = F)
# Casualties by year
casualties = gtd %>% group_by(iyear) %>% summarise(Casualties = sum(nkill, na.rm = T))
ggplot(data = casualties, aes(x = iyear, y = Casualties, fill = iyear)) + geom_bar(stat = 'identity') +
ggtitle('Casualties from Terrorist Attacks from 1970 to 2014') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
# Correlation between casualties and attacks
cor(casualties, attacks_per_year)
# Casualties per year by attack type (Armed Assault, Bombing/Explosion, Assassination)
casulaties_attack_type2 = gtd %>%
filter(attacktype1_txt == c('Armed Assault', 'Assassination', 'Bombing/Explosion')) %>%
group_by(iyear, attacktype1_txt) %>%
summarise(Casualties = sum(nkill, na.rm = T))
ggplot(data = casulaties_attack_type2, aes(x = iyear, y = Casualties, fill = attacktype1_txt)) +
geom_bar(stat = 'identity', position = 'dodge') +
ggtitle('Casualties from Attack Type by Year') + xlab('Year') +
scale_fill_brewer(palette = 'RdBu', name = '') + theme_solarized(light = F)
# Attacks in global elite cities
global_elite = c('New York City', 'Los Angeles', 'Chicago', 'Toronto', 'San Francisco',
'Boston', 'London', 'Paris', 'Brussels', 'Berlin', 'Amsterdam',
'Tokyo', 'Singapore', 'Seoul', 'Sydney', 'Melbourne')
global_elite_attacks = gtd %>% filter(city %in% global_elite) %>%
group_by(iyear) %>% summarise(Attacks = n())
ggplot(data = global_elite_attacks, aes(x = iyear, y = Attacks, fill = iyear)) +
geom_bar(stat = 'identity') + ggtitle('Terrorist Attacks in Global Elite Cities from 1970 to 2014') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
mean(global_elite_attacks$Attacks)
# Top 16 cities by most number of attacks
top_cities = gtd %>% group_by(city) %>% summarise(Attacks = n()) %>%
arrange(desc(Attacks))
top_16_cities = top_n(top_cities, 17)[-1,]
top_16_cities_attacks = gtd %>% filter(city %>% top_16_cities$city) %>%
group_by(iyear) %>% summarise(Attacks = n())
ggplot(data = top_16_cities_attacks, aes(x = iyear, y = Attacks, fill = iyear)) +
geom_bar(stat = 'identity') + ggtitle('Top 16 Cities by Number of Terrorist Attacks from 1970 to 2014') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
mean(top_16_cities_attacks$Attacks)
# Casualties in global elite cities
global_elite_casualties = gtd %>% filter(city %in% global_elite) %>%
group_by(iyear) %>% summarise(Casualties = sum(nkill, na.rm = T))
ggplot(data = global_elite_casualties, aes(x = iyear, y = Casualties, fill = iyear)) + geom_bar(stat = 'identity') +
ggtitle('Terrorist Attack Casualties in Global Elite Cities') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
mean(global_elite_casualties$Casualties)
# Casualties in top 16 cities
top_16_cities_casualties = gtd %>% filter(city %in% top_16_cities$city) %>%
group_by(iyear) %>% summarise(Casualties = sum(nkill, na.rm = T))
ggplot(data = top_16_cities_casualties, aes(x = iyear, y = Casualties, fill = iyear)) +
geom_bar(stat = 'identity') + ggtitle('Terrorist Attack Casualties in Top 16 Cities') +
xlab('Year') + scale_fill_gradient(low = 'red', high = 'red4') + guides(fill = F) +
theme_solarized(light = F)
mean(top_16_cities_casualties$Casualties)
# Choropleth of attacks
world_map = map_data('world')
world_map = subset(world_map, region!="Antarctica")
attacks_per_country = gtd %>% group_by(region = country_txt) %>% summarise(Attacks = n())
attacks_per_country$region[attacks_per_country$region == 'United States'] = 'USA'
ggplot(data = attacks_per_country) +
geom_map(data = world_map, map = world_map, aes(map_id = region), fill = 'white',
color = "#7f7f7f", size = 0.25) + geom_map(map = world_map,
aes(map_id = region, fill = Attacks), size=0.25) + scale_fill_gradient(low="#fff7bc",
high="#cc4c02", name="Terrorist Attacks") + expand_limits(x = world_map$long, y = world_map$lat) +
labs(x="", y="", title="Terrorist Attacks by Country") +
theme(panel.grid=element_blank(), panel.border=element_blank()) +
theme(axis.ticks=element_blank(), axis.text=element_blank()) +
theme(legend.position="top", legend.key.size = unit(1, 'cm'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment