Importing the libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.2     ✔ purrr   1.0.1
## ✔ tibble  3.2.1     ✔ dplyr   1.1.2
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.3     ✔ forcats 0.5.2
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.2.3
library(treemap)
## Warning: package 'treemap' was built under R version 4.2.3
library(rmarkdown)
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(viridis)
## Loading required package: viridisLite
library(ggplot2)
library(tidyr)
library(dplyr)
library(viridisLite)
library(RColorBrewer)
library(countrycode)
## Warning: package 'countrycode' was built under R version 4.2.3
library(wordcloud)
## Warning: package 'wordcloud' was built under R version 4.2.3
library(tidytext)
## Warning: package 'tidytext' was built under R version 4.2.3
library(tm)
## Warning: package 'tm' was built under R version 4.2.3
## Loading required package: NLP
## 
## Attaching package: 'NLP'
## 
## The following object is masked from 'package:ggplot2':
## 
##     annotate
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.3
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

Importing the Dataset

data = read.csv("D:/SOBAT KARIER/PORTOFOLIO/Terrorist/globalterrorismdb.csv")
str(data)
## 'data.frame':    181691 obs. of  135 variables:
##  $ eventid           : num  1.97e+11 1.97e+11 1.97e+11 1.97e+11 1.97e+11 ...
##  $ iyear             : int  1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ...
##  $ imonth            : int  7 0 1 1 1 1 1 1 1 1 ...
##  $ iday              : int  2 0 0 0 0 1 2 2 2 3 ...
##  $ approxdate        : chr  "" "" "" "" ...
##  $ extended          : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ resolution        : chr  "" "" "" "" ...
##  $ country           : int  58 130 160 78 101 217 218 217 217 217 ...
##  $ country_txt       : chr  "Dominican Republic" "Mexico" "Philippines" "Greece" ...
##  $ region            : int  2 1 5 8 4 1 3 1 1 1 ...
##  $ region_txt        : chr  "Central America & Caribbean" "North America" "Southeast Asia" "Western Europe" ...
##  $ provstate         : chr  "" "Federal" "Tarlac" "Attica" ...
##  $ city              : chr  "Santo Domingo" "Mexico city" "Unknown" "Athens" ...
##  $ latitude          : num  18.5 19.4 15.5 38 33.6 ...
##  $ longitude         : num  -70 -99.1 120.6 23.8 130.4 ...
##  $ specificity       : int  1 1 4 1 1 1 1 1 1 1 ...
##  $ vicinity          : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ location          : chr  "" "" "" "" ...
##  $ summary           : chr  "" "" "" "" ...
##  $ crit1             : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ crit2             : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ crit3             : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ doubtterr         : int  0 0 0 0 -9 0 0 1 0 0 ...
##  $ alternative       : int  NA NA NA NA NA NA NA 2 NA NA ...
##  $ alternative_txt   : chr  "" "" "" "" ...
##  $ multiple          : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ success           : int  1 1 1 1 1 1 0 1 1 1 ...
##  $ suicide           : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ attacktype1       : int  1 6 1 3 7 2 1 3 7 7 ...
##  $ attacktype1_txt   : chr  "Assassination" "Hostage Taking (Kidnapping)" "Assassination" "Bombing/Explosion" ...
##  $ attacktype2       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ attacktype2_txt   : chr  "" "" "" "" ...
##  $ attacktype3       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ attacktype3_txt   : chr  "" "" "" "" ...
##  $ targtype1         : int  14 7 10 7 7 3 3 21 4 2 ...
##  $ targtype1_txt     : chr  "Private Citizens & Property" "Government (Diplomatic)" "Journalists & Media" "Government (Diplomatic)" ...
##  $ targsubtype1      : int  68 45 54 46 46 22 25 107 28 21 ...
##  $ targsubtype1_txt  : chr  "Named Civilian" "Diplomatic Personnel (outside of embassy, consulate)" "Radio Journalist/Staff/Facility" "Embassy/Consulate" ...
##  $ corp1             : chr  "" "Belgian Ambassador Daughter" "Voice of America" "" ...
##  $ target1           : chr  "Julio Guzman" "Nadine Chaval, daughter" "Employee" "U.S. Embassy" ...
##  $ natlty1           : int  58 21 217 217 217 217 218 217 217 217 ...
##  $ natlty1_txt       : chr  "Dominican Republic" "Belgium" "United States" "United States" ...
##  $ targtype2         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ targtype2_txt     : chr  "" "" "" "" ...
##  $ targsubtype2      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ targsubtype2_txt  : chr  "" "" "" "" ...
##  $ corp2             : chr  "" "" "" "" ...
##  $ target2           : chr  "" "" "" "" ...
##  $ natlty2           : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ natlty2_txt       : chr  "" "" "" "" ...
##  $ targtype3         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ targtype3_txt     : chr  "" "" "" "" ...
##  $ targsubtype3      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ targsubtype3_txt  : chr  "" "" "" "" ...
##  $ corp3             : chr  "" "" "" "" ...
##  $ target3           : chr  "" "" "" "" ...
##  $ natlty3           : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ natlty3_txt       : chr  "" "" "" "" ...
##  $ gname             : chr  "MANO-D" "23rd of September Communist League" "Unknown" "Unknown" ...
##  $ gsubname          : chr  "" "" "" "" ...
##  $ gname2            : chr  "" "" "" "" ...
##  $ gsubname2         : chr  "" "" "" "" ...
##  $ gname3            : chr  "" "" "" "" ...
##  $ gsubname3         : chr  "" "" "" "" ...
##  $ motive            : chr  "" "" "" "" ...
##  $ guncertain1       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ guncertain2       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ guncertain3       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ individual        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nperps            : int  NA 7 NA NA NA -99 3 -99 1 1 ...
##  $ nperpcap          : num  NA NA NA NA NA -99 NA -99 1 1 ...
##  $ claimed           : int  NA NA NA NA NA 0 NA 0 1 0 ...
##  $ claimmode         : int  NA NA NA NA NA NA NA NA 1 NA ...
##  $ claimmode_txt     : chr  "" "" "" "" ...
##  $ claim2            : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ claimmode2        : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ claimmode2_txt    : chr  "" "" "" "" ...
##  $ claim3            : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ claimmode3        : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ claimmode3_txt    : chr  "" "" "" "" ...
##  $ compclaim         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weaptype1         : int  13 13 13 6 8 5 5 6 8 8 ...
##  $ weaptype1_txt     : chr  "Unknown" "Unknown" "Unknown" "Explosives" ...
##  $ weapsubtype1      : int  NA NA NA 16 NA 5 2 16 19 20 ...
##  $ weapsubtype1_txt  : chr  "" "" "" "Unknown Explosive Type" ...
##  $ weaptype2         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weaptype2_txt     : chr  "" "" "" "" ...
##  $ weapsubtype2      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weapsubtype2_txt  : chr  "" "" "" "" ...
##  $ weaptype3         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weaptype3_txt     : chr  "" "" "" "" ...
##  $ weapsubtype3      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weapsubtype3_txt  : chr  "" "" "" "" ...
##  $ weaptype4         : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weaptype4_txt     : chr  "" "" "" "" ...
##  $ weapsubtype4      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ weapsubtype4_txt  : chr  "" "" "" "" ...
##  $ weapdetail        : chr  "" "" "" "Explosive" ...
##  $ nkill             : int  1 0 1 NA NA 0 0 0 0 0 ...
##   [list output truncated]

Terrorism

countries <- data %>% 
  group_by(country_txt) %>%
  summarise(Total = n())
names(countries) <- c("country.name", "total")
countries$iso3 = countrycode(countries$country.name, origin = "country.name", destination = "iso3c")
## Warning: Some values were not matched unambiguously: Czechoslovakia, East Germany (GDR), International, Kosovo, North Yemen, Serbia-Montenegro, South Vietnam, South Yemen, Yugoslavia
stops <- data.frame(q = c(0, exp(1:10)/exp(10)),
                        c = substring(viridis(10 + 1, option = "D"), 0, 7)) %>%  
  list_parse2()

data(worldgeojson, package = "highcharter")
hc_plot = highchart() %>% 
          hc_add_series_map(worldgeojson, countries, value = "total", joinBy = "iso3") %>% 
          hc_colorAxis(stops = stops) %>% 
          hc_legend(enabled = TRUE) %>% 
          hc_add_theme(hc_theme_db()) %>% 
          hc_mapNavigation(enabled = TRUE) %>%
          hc_title(text = "Total Number of Attacks From 1970 to 2017") %>%
          hc_add_theme(hc_theme_google())
hc_plot

Terrosism over the years

year_killing <- data %>%
  group_by(Year = iyear) %>%
  filter(nkill > 0) %>%
  summarise(total_killing = sum(nkill)) %>%
  ungroup()

hc_plot <-  highchart() %>% 
  hc_add_series(year_killing, hcaes(x = Year, y = total_killing), type = "line") %>%
  hc_tooltip(crosshairs = TRUE, borderWidth = 1.5, headerFormat = "", pointFormat = paste("Year: <b>{point.Year}</b> <br>","Killing: <b>{point.total_killing}</b><br>")) %>%
  hc_title(text = "Terrorism over the years") %>% 
  hc_subtitle(text = "1970-2017") %>%
  hc_xAxis(title = list(text = "Year")) %>%
  hc_yAxis(title = list(text = "Number of people killed"),
           allowDecimals = FALSE,
           plotLines = list(list(
             color = "black", width = 1, dashStyle = "Dash",
             value = mean(year_killing$total_killing),
             label = list(text = "Average people killed = 8763",
             style = list(color = 'red', fontSize = 20))))) %>% 
  hc_add_theme(hc_theme_ffx())
hc_plot

Countries effected by the terrorism

country1 <- data %>%
  group_by(country_txt) %>%
  summarise(incidents = n()) %>%
  arrange(desc(incidents))
treemap(country1, 
        index=c("country_txt"), 
        vSize = "incidents",  
        palette = "RdYlBu",  
        title="Number of events - Terrorism", 
        fontsize.title = 16 
)

Citites Effected by the terrorism

killings_city <- data %>%
  drop_na(nkill) %>%
  filter(nkill > 1) %>%
  group_by(city)%>%
  summarise(total_killing = sum(nkill)) %>%
  arrange(desc(total_killing)) %>%
  head(n=15)

ggplot(killings_city, aes(x=reorder(city, total_killing), y= total_killing)) +
  geom_bar(stat = "identity", fill = "red", col = "black") +
  coord_flip() +
  labs(x="City", y="Killings", title = "Number of people killed - CITY")

Number Of terrorists killed from 1970 to 2016

killter <- data %>%
  filter(nkillter > 0) %>%
  group_by(region = region_txt, year = iyear) %>%
  summarise(terrorist_killed = sum(nkillter))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
ggplot(killter, aes(x=reorder(year, terrorist_killed), y=terrorist_killed)) +
  geom_bar(stat = "identity", fill = "red") +
  coord_flip() +
  labs(x="Year", y="Terrorist killed", title = "Number of terrorist killed from 1970 - 2016")

Number of Killings - Regionwise

region <- data %>%
  group_by(region = region_txt) %>%
  filter(nkill > 0) %>%
  summarise(killings = sum(nkill)) %>%
  arrange(desc(killings))
   
hc_plot =   hctreemap2(region,
    group_vars = "region",
    size_var = "killings",
    color_var = "killings",
    layoutAlgorithm = "squarified",
    levelIsConstant = FALSE,
    levels = list(
      list(level = 1, dataLabels = list(enabled = TRUE)),
      list(level = 2, dataLabels = list(enabled = FALSE)),
      list(level = 3, dataLabels = list(enabled = FALSE))
    )
  ) %>% 
  hc_colorAxis(minColor = brewer.pal(7, "Reds")[1],
               maxColor = brewer.pal(7, "Reds")[7]) %>% 
  hc_tooltip(pointFormat = "<b>{point.region}</b>:<br>
             killings: {point.colorValue:,.0f}")
## Warning: 'hctreemap2' is deprecated.
## Use 'data_to_hierarchical' instead.
## See help("Deprecated")
hc_plot

Terrorist group Involved

gang_killing <- data %>%
  group_by(gang = gname) %>%
  summarise(killing = sum(nkill)) %>%
  arrange(desc(killing)) %>%
  head(n=20)

ggplot(gang_killing, aes(x=reorder(gang, killing), y = killing)) +
  geom_bar(fill = 'red', col = "black", stat = "identity") +
  theme_classic() +
  labs(x="Gangs", y="Number of people killed") +
  coord_flip()

Terrorist nationality

terrorist <- data %>%
  filter(nkill > 0) %>%
  group_by(natlty1_txt) %>%
  summarise(killing = sum(nkill)) %>%
  arrange(desc(killing)) %>%
  head(n = 10)

ggplot(terrorist, aes(x=reorder(natlty1_txt, killing), y = killing)) +
  geom_bar(fill = 'red', col = "black", stat = "identity") +
  theme_classic() +
  labs(x="Nationality of Terrorists", y="Number of people killed") +
  coord_flip()

Crime Types

crime <- data %>%
  filter(nkill > 0) %>%
  group_by(alternative_txt) %>%
  summarise(killing = sum(nkill)) %>%
  arrange(desc(killing))

ggplot(crime, aes(x=reorder(alternative_txt, killing), y = killing)) +
  geom_bar(fill = 'red', col = "black", stat = "identity") +
  theme_classic() +
  labs(x="Crime Type", y="Number of people killed") +
  coord_flip()

Crime Types

weapon <- data %>%
  filter(weaptype1_txt != "Unknown") %>%
  group_by(weaptype1_txt) %>%
  summarise(Events = n())

plot1 <- ggplot(weapon, aes(x = reorder(weaptype1_txt, Events), y = Events)) +
  geom_bar(stat = "identity", fill = "Red", col = "black") +
  labs(x = "Weapon Used", y = "Number of times", title = "Weapon Type") +
  coord_flip() +
  theme_classic()

plot1

Target Terrorists

target <- data %>%
  group_by(targtype1_txt) %>%
  summarise(Events = n())

plot4 <- ggplot(target, aes(x = reorder(targtype1_txt, Events), y = Events)) +
  geom_bar(stat = "identity",col = "black", fill = "darkblue") +
  coord_flip() +
  labs(x = "Target", y = "Events", title = "Target")

plot4