####MBG###
library(ggplot2)
library(dplyr)
library(readr)
terrorismo <- read_csv("globalterrorismdb_0718dist.csv")
#str(terrorismo)
year <- as.factor(terrorismo$iyear)
month <- as.factor(terrorismo$imonth)
day <- as.factor(terrorismo$iday)
country<-as.factor(terrorismo$country_txt)
city<- as.factor(terrorismo$city)
terrorismo$region_txt = gsub("&", "and", terrorismo$region_txt)
region<- as.factor(terrorismo$region_txt)
attacktype<-as.factor(terrorismo$attacktype1_txt)
weaptype<-as.factor(terrorismo$weaptype1_txt)
gname<-as.factor(terrorismo$gname)
motive<-as.factor(terrorismo$motive)
rm(motive)
nwound<-as.numeric(terrorismo$nwound)
nkill<-as.numeric(terrorismo$nkill)
victims<- nkill+ nwound
targtype<-as.factor(terrorismo$targtype1_txt)
propextent<-as.factor(terrorismo$propextent_txt)
rm(propextent)
propvalue<-as.numeric(terrorismo$propvalue)
terrorismo$gname<- as.character(terrorismo$gname)
terrorismo$gname = gsub("Autónomo", "Autonomo", terrorismo$gname)
terrorismo$gname = gsub("Argandoña", "Argandona", terrorismo$gname)
gname<-as.factor(terrorismo$gname)
terror <- data.frame(year, country, attacktype, weaptype, gname, victims, nwound,
nkill, targtype, targtype, propvalue, city, region)
region_ordered<- terror %>% count(region) %>% arrange(desc(n))
str(region)
#dashboard: sort by year y region
#-----------------------PLOTS-----------------------------------
#Accidents per year
###geom_line for accidents growth
ggplot(terror, aes(year)) + geom_bar(stat = "count")
#Accidents per region
####top5
ggplot(terror, aes(region)) + geom_bar(stat = "count") + coord_flip()
#Weapons used on Attacks
####top 5
levels(terror$weaptype)[levels(terror$weaptype)=="Vehicle (not to include vehicle-borne explosives, i.e., car or truck bombs)"] <- "Vehicle"
ggplot(terror, aes(weaptype)) + geom_bar(stat = "count") + coord_flip()
#Type of attacks
###top 5
ggplot(terror, aes(attacktype)) + geom_bar(stat = "count") + coord_flip()
#Value of Damage caused by Atacks
####fix values off scale
boxplot(terror$propvalue)
ggplot(terror, aes(year, propvalue)) + geom_point()
#Number of Victims of Atacks (wounded+killed) per year
###nicer fill
ggplot(terror, aes(year, victims, fill=nkill)) + geom_bar(stat = "identity")
#Attacks per Terrorist Groups
####top 5 because there're too many terrorist groups
ggplot(terror, aes(gname)) + geom_bar(stat = "count") + coord_flip()
ggplot(head(terror), aes(gname)) + geom_bar(stat = "count") + coord_flip()