The finished map

Mapping Terror Attacks with Highcharter

A few days ago someone asked me whether I could visualize the terror attacks of recent years in Europe. Here is an attempt to do exactly this.

The code for this blogpost can be found in this GitHub Repository.

Getting Started

First I need data about terror attacks. For this I downloaded the Global Terrorism Database.

Now in Rstudio, I load in the needed packages:

pacman::p_load(tidyverse, magrittr, readxl, highcharter)

After I put the downloaded .xlsx file into my working directory, I use readxl to read in the data.

terror <- read_xlsx("data/globalterrorism.xlsx", sheet = "Data")

Now comes the data wrangling part.

terror_map <- terror %>% 
  filter(iyear >= 2000) %>% 
  filter(region %in% c(8, 9)) %>% 
  filter(nkill > 0) %>% 
  filter(longitude <= 38) %>% 
  # filter(longitude >= 24) %>% 
  # filter(latitude >= 13) %>% 
  arrange(desc(nkill)) %>% 
  mutate(lon = longitude) %>% 
  mutate(lat = latitude) %>% 
  mutate(date = paste(iyear, imonth, iday, sep = "-")) %>% 
  mutate(name = paste0("<br> <b>Country:</b> ", country_txt,
                       "<br> <b>City:</b> ", city, 
                       "<br> <b>Date:</b> ", date,
                       "<br> <b>Group:</b> ", gname,
                       "<br> <b>Attack Type:</b> ", attacktype1_txt,
                       "<br> <b>Target:</b> ", target1,
                       "<br> <b>Death Count</b>")) %>% 
  select(name, lon = longitude, lat,  z = nkill)

And finally, I apply the highcharter functions to create a beautiful interactive map.

hcmap("custom/europe", showInLegend = FALSE) %>%
  hc_add_series(data = na.omit(terror_map), type = "mapbubble",
                minSize = 0, maxSize = 30,
                name = "Terror Attack", fillOpacity = 0.01) %>%
  highcharter::hc_title(text = "Terror Attacks with Casualties > 1 (2000 - 2016)", 
                        style = list(fontWeight = "bold")) %>%
  hc_subtitle(text = "Source: <a href='https://www.start.umd.edu/gtd/'>Global Terrorism Database (GTD)</a>")

The result:

Click here for interactive version.

Related

comments powered by Disqus