Fatal events in the Sahel

In this short post, we will show how to use the rhdx, dplyr, purrr, sf and gganimate R packages to show the number of fatal incidents in 5 Sahelian countries. The rhdx package is not yet on CRAN, so you will need to use the remotes package to install it first:

remotes::install_gitlab("dickoa/rhxl") ## rhdx dependency
remotes::install_gitlab("dickoa/rhdx") ## github mirror also avalailable

gganimate depends on the gifski R package and to install it, make sure you first have the gifski Rust cargo crate installed on your system.

install.packages("gifski")

This analysis was inspired by this tweet by José Luengo-Cabrera, researcher at the Crisis Group.

Our visualization will be done for the following countries in the Sahel : Burkina Faso, Mali, Chad, Mauritania and Niger. There is a lot of insecurity and conflicts in these countries that resulted in the death of several thousands of people.

library(tidyverse)
library(sf)
library(rhdx)
library(gganimate)

The goal of this post is to visualize the number of fatal events in theses countries between 2012 and 2018 using an animated map. In order to do that, will need to get the administrative boundaries for these countries. We can get the latest boundaries validated by the governments and the humanitarian community directly from HDX using the rhdx package. We will use rhdx::pull_dataset function and use the name of the dataset of interest as value. rhdx::get_resource and rhdx::download_resource allow to respectively get the a resource by its index and read the data into memory.

set_rhdx_config()

wca <- pull_dataset("west-and-central-africa-administrative-boundaries-levels") %>%
  get_resource(1) %>%
  read_resource()
## reading layer: wca_adm0
glimpse(wca)
## Observations: 24
## Variables: 6
## $ OBJECTID   <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
## $ admin0Name <chr> "Benin", "Burkina Faso", "Cabo Verde", "Cameroon", "C…
## $ admin0Pcod <chr> "BJ", "BF", "CV", "CM", "CF", "TD", "CI", "CD", "GQ",…
## $ Shape_Leng <dbl> 19.027187, 31.489997, 13.599357, 47.520296, 49.366900…
## $ Shape_Area <dbl> 9.52054910, 22.71996765, 0.34276153, 38.08645238, 50.…
## $ geometry   <MULTIPOLYGON [°]> MULTIPOLYGON (((2.890491 12..., MULTIPOL…

wca is a Simple Feature and we can manipulate it using sf and dplyr. The data downloaded from HDX covers the 24 countries of West and Central Africa, we will filter the data to extract the 5 countries of interest.

g5_ab <- wca %>%
  filter(admin0Pcod %in% c("BF", "ML", "NE", "MR", "TD"))

We can now check our data by plotting it

g5_ab %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

Now that we have our background map, the next step is to get the conflict data. One of the main source for conflict data in the Sahel is ACLED and it can also be accessed from HDX. We will use the rhdx::search_datasets function since it allows to access multiple datasets as list. HDX is based on CKAN whose search is powered by SOLR. SOLR can be used to build complex queries to get exactly the datasets we want. In our case, we want conflict data from the ACLED organization in HDX and from the 5 countries (group in CKAN API)

solr_query <- "organization:acled AND groups:(mli OR bfa OR tcd OR mrt OR ner)"
g5_acled <- search_datasets(query = "conflict data",
                            fq = solr_query)
g5_acled
## [[1]]
## <HDX Dataset> 331be608-def1-45c5-a9b0-36d4b3b4197e 
##   Title: Chad - Conflict Data
##   Name: acled-data-for-chad
##   Date: 01/01/1997-12/31/2019
##   Tags (up to 5): conflicts, hxl, political violence, protests
##   Locations (up to 5): tcd
##   Resources (up to 5): Conflict Data for Chad
## 
## [[2]]
## <HDX Dataset> 653dc159-097d-4fef-9527-53ee30d132ff 
##   Title: Niger - Conflict Data
##   Name: acled-data-for-niger
##   Date: 01/01/1997-12/31/2019
##   Tags (up to 5): conflicts, hxl, political violence, protests
##   Locations (up to 5): ner
##   Resources (up to 5): Conflict Data for Niger
## 
## [[3]]
## <HDX Dataset> 537ee9a6-ff76-4e00-a9f6-1c7c16ae1628 
##   Title: Mauritania - Conflict Data
##   Name: acled-data-for-mauritania
##   Date: 01/01/1997-12/31/2019
##   Tags (up to 5): conflicts, hxl, political violence, protests
##   Locations (up to 5): mrt
##   Resources (up to 5): Conflict Data for Mauritania
## 
## [[4]]
## <HDX Dataset> 5895de63-010c-4716-97cb-fbdd3caf4e3a 
##   Title: Mali - Conflict Data
##   Name: acled-data-for-mali
##   Date: 01/01/1997-12/31/2019
##   Tags (up to 5): conflicts, hxl, political violence, protests
##   Locations (up to 5): mli
##   Resources (up to 5): Conflict Data for Mali
## 
## [[5]]
## <HDX Dataset> 6913ddaf-1ad2-4cad-b178-592b6d49cd61 
##   Title: Burkina Faso - Conflict Data
##   Name: acled-data-for-burkina-faso
##   Date: 01/01/1997-12/31/2019
##   Tags (up to 5): conflicts, hxl, political violence, protests
##   Locations (up to 5): bfa
##   Resources (up to 5): Conflict Data for Burkina Faso
## 
## [[6]]
## <HDX Dataset> 71d852e4-e41e-4320-a770-9fc2bb87fb64 
##   Title: ACLED Conflict Data for Africa 1997-2016
##   Name: acled-conflict-data-for-africa-1997-lastyear
##   Date: 01/01/2017
##   Tags (up to 5): conflict, coordinates, geodata, political violence, protection
##   Locations (up to 5): dza, ago, ben, bwa, bfa
##   Resources (up to 5): ACLED-Version-7-All-Africa-1997-2016_csv_dyadic-file.zip, ACLED-Version-7-All-Africa-1997-2016_dyadic-file.xlsx, ACLED-Version-7-All-Africa-1997-2016_monadic-file_csv.zip, ACLED-Version-7-All-Africa-1997-2016_monadic-file-1.xlsx, ACLED-Version-7-All-Africa-1997-2016_actordyad_csv.zip
## 
## attr(,"class")
## [1] "datasets_list"

We will select the first 5 datasets (our 5 countries) in the list of datasets and bind them together using purrr::map_df and a helper function.

g5_acled <- g5_acled[1:5] ## pick the first 5 the 6th is the Africa wide dataset

## create a helper function to read resources from each dataset
read_acled_data <- function(dataset) {
  dataset %>%
    get_resource(1) %>%
    read_resource(force_download = TRUE)
}

g5_acled_data <- map_df(g5_acled, read_acled_data)
glimpse(g5_acled_data)
## Observations: 6,088
## Variables: 30
## $ data_id          <dbl> 3124276, 3124273, 3124274, 3124275, 3124272, 30…
## $ iso              <dbl> 148, 148, 148, 148, 148, 148, 148, 148, 148, 14…
## $ event_id_cnty    <chr> "CHA805", "CHA804", "CHA802", "CHA803", "CHA801…
## $ event_id_no_cnty <dbl> 805, 804, 802, 803, 801, 800, 799, 798, 796, 79…
## $ event_date       <date> 2019-02-20, 2019-02-19, 2019-02-19, 2019-02-19…
## $ year             <dbl> 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,…
## $ time_precision   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1,…
## $ event_type       <chr> "Violence against civilians", "Riots/Protests",…
## $ actor1           <chr> "Boko Haram - Wilayat Gharb Ifriqiyyah", "Prote…
## $ assoc_actor_1    <chr> NA, "MPS: Patriotic Salvation Movement", NA, NA…
## $ inter1           <dbl> 2, 6, 2, 2, 6, 6, 6, 6, 6, 6, 2, 8, 8, 6, 8, 3,…
## $ actor2           <chr> "Civilians (Chad)", NA, "Civilians (Chad)", "Ci…
## $ assoc_actor_2    <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ inter2           <dbl> 7, 0, 7, 7, 1, 0, 0, 0, 0, 1, 1, 2, 2, 0, 2, 7,…
## $ interaction      <dbl> 27, 60, 27, 27, 16, 60, 60, 60, 60, 16, 12, 28,…
## $ region           <chr> "Middle Africa", "Middle Africa", "Middle Afric…
## $ country          <chr> "Chad", "Chad", "Chad", "Chad", "Chad", "Chad",…
## $ admin1           <chr> "Lac", "Ouaddai", "Lac", "Lac", "N'Djamena", "N…
## $ admin2           <chr> "Kaya", "Ouara", "Wayi", "Fouli", "N'Djamena", …
## $ admin3           <chr> "Kaya", "Kachimel-Abeche", "Wayi", "Daboua", "N…
## $ location         <chr> "Hougou", "Abeche", "Bourboura", "Ngacha", "NDj…
## $ latitude         <dbl> 13.4767, 13.8290, 13.5015, 13.6547, 12.1085, 12…
## $ longitude        <dbl> 14.0323, 20.8320, 15.4459, 13.8206, 15.0482, 15…
## $ geo_precision    <dbl> 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 2, 2, 1, 2, 1,…
## $ source           <chr> "UN (unspecified)", "Alwihda (Chad)", "Alwihda …
## $ source_scale     <chr> "Other", "National", "Other-National", "Other-N…
## $ notes            <chr> "20 February. Boko Haram attack the village of …
## $ fatalities       <dbl> 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, …
## $ timestamp        <dbl> 1551122597, 1551122597, 1551122597, 1551122597,…
## $ iso3             <chr> "TCD", "TCD", "TCD", "TCD", "TCD", "TCD", "TCD"…

We have all the data we need for our analysis, we just need to aggregate total number fatalities by geographical coordinates, countries and year.

g5_acled_fatalities_loc <- g5_acled_data %>%
  filter(year %in% 2012:2018, fatalities > 0) %>%
  group_by(year, country, latitude, longitude) %>%
  summarise(total_fatalities = sum(fatalities, na.rm = TRUE)) %>%
  arrange(year) %>%
  ungroup()

We can finally use our boundaries (g5_ab), the conflict data (g5_acled_fatalities_loc) with gganimate to dynamically visualize the different fatal incidents in G5 Sahel countries.

g5_ab %>%
  ggplot() +
  geom_sf(fill = "#383838", color = "gray") +
  coord_sf(datum = NA) +
  geom_point(data = g5_acled_fatalities_loc,
             aes(x = longitude,
                 y = latitude,
                 size = total_fatalities,
                 fill = total_fatalities),
             shape = 21,
             color = "transparent") +
  scale_fill_viridis_c(option = "plasma") +
  geom_sf_text(aes(label = admin0Name), color = "gray", fontface = "bold") +
  labs(x = "",
       y = "",
       title = "Fatal events in the Sahel G5",
       subtitle = "for the year {current_frame}",
       caption = "source: ACLED") +
  theme_void() +
  theme(legend.position = "none") +
  transition_manual(year, cumulative = TRUE) +
  shadow_mark()

Session info for this analysis.

Session info

devtools::session_info()
##  Session info ──────────────────────────────────────────────────────────
##  setting  value                                      
##  version  R version 3.5.2 Patched (2019-02-05 r76078)
##  os       Arch Linux                                 
##  system   x86_64, linux-gnu                          
##  ui       X11                                        
##  language                                            
##  collate  en_US.UTF-8                                
##  ctype    en_US.UTF-8                                
##  tz       Africa/Dakar                               
##  date     2019-03-04                                 
## 
##  Packages ──────────────────────────────────────────────────────────────
##  package     * version    date       lib source                        
##  assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.0)                
##  backports     1.1.3      2018-12-14 [1] CRAN (R 3.5.2)                
##  blogdown      0.10       2019-01-09 [1] CRAN (R 3.5.2)                
##  bookdown      0.9        2018-12-21 [1] CRAN (R 3.5.2)                
##  broom         0.5.1      2018-12-05 [1] CRAN (R 3.5.1)                
##  callr         3.1.1      2018-12-21 [1] CRAN (R 3.5.2)                
##  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.5.0)                
##  class         7.3-15     2019-01-01 [1] CRAN (R 3.5.2)                
##  classInt      0.3-1      2018-12-18 [1] CRAN (R 3.5.2)                
##  cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.1)                
##  colorspace    1.4-0      2019-01-13 [1] CRAN (R 3.5.2)                
##  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.0)                
##  crul          0.7.0      2019-01-03 [1] Github (ropensci/crul@bb12ce1)
##  curl          3.3        2019-01-10 [1] CRAN (R 3.5.2)                
##  DBI           1.0.0.9001 2019-03-01 [1] Github (r-dbi/DBI@2bbe284)    
##  desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.0)                
##  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.1)                
##  digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.1)                
##  dplyr       * 0.8.0.1    2019-02-15 [1] CRAN (R 3.5.2)                
##  e1071         1.7-0.1    2019-01-21 [1] CRAN (R 3.5.2)                
##  evaluate      0.13       2019-02-12 [1] CRAN (R 3.5.2)                
##  fansi         0.4.0      2018-12-31 [1] Github (brodieG/fansi@ab11e9c)
##  farver        1.1.0      2018-11-20 [1] CRAN (R 3.5.2)                
##  forcats     * 0.4.0      2019-02-17 [1] CRAN (R 3.5.2)                
##  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.1)                
##  generics      0.0.2      2018-11-29 [1] CRAN (R 3.5.1)                
##  gganimate   * 1.0.1      2019-02-15 [1] CRAN (R 3.5.2)                
##  ggplot2     * 3.1.0      2018-10-25 [1] CRAN (R 3.5.1)                
##  glue          1.3.0      2018-07-17 [1] CRAN (R 3.5.1)                
##  gtable        0.2.0      2016-02-26 [1] CRAN (R 3.5.0)                
##  haven         2.1.0      2019-02-19 [1] CRAN (R 3.5.2)                
##  hms           0.4.2      2018-03-10 [1] CRAN (R 3.5.0)                
##  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.5.0)                
##  httpcode      0.2.0      2016-11-14 [1] CRAN (R 3.5.0)                
##  httr          1.4.0      2018-12-11 [1] CRAN (R 3.5.2)                
##  jsonlite      1.6        2018-12-07 [1] CRAN (R 3.5.2)                
##  knitr         1.21       2018-12-10 [1] CRAN (R 3.5.2)                
##  lattice       0.20-38    2018-11-04 [1] CRAN (R 3.5.2)                
##  lazyeval      0.2.1      2017-10-29 [1] CRAN (R 3.5.0)                
##  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.0)                
##  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.0)                
##  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.5.0)                
##  modelr        0.1.4      2019-02-18 [1] CRAN (R 3.5.2)                
##  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.5.0)                
##  nlme          3.1-137    2018-04-07 [1] CRAN (R 3.5.2)                
##  pillar        1.3.1.9000 2019-01-22 [1] Github (r-lib/pillar@3a54b8d) 
##  pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.1)                
##  pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.5.1)                
##  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.1)                
##  plyr          1.8.4      2016-06-08 [1] CRAN (R 3.5.0)                
##  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.0)                
##  processx      3.2.1      2018-12-05 [1] CRAN (R 3.5.1)                
##  progress      1.2.0      2018-06-14 [1] CRAN (R 3.5.1)                
##  ps            1.3.0      2018-12-21 [1] CRAN (R 3.5.2)                
##  purrr       * 0.3.0      2019-01-27 [1] CRAN (R 3.5.2)                
##  R6            2.4.0      2019-02-14 [1] CRAN (R 3.5.2)                
##  Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.1)                
##  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.5.2)                
##  readxl        1.3.0      2019-02-15 [1] CRAN (R 3.5.2)                
##  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.1)                
##  rhdx        * 0.0.1.9000 2019-02-26 [1] local                         
##  rhxl          0.1.0.9000 2018-09-04 [1] Github (dickoa/rhxl@eea6dbc)  
##  rlang         0.3.1      2019-01-08 [1] CRAN (R 3.5.2)                
##  rmarkdown     1.11       2018-12-08 [1] CRAN (R 3.5.2)                
##  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.0)                
##  rstudioapi    0.9.0      2019-01-09 [1] CRAN (R 3.5.2)                
##  rvest         0.3.2      2016-06-17 [1] CRAN (R 3.5.0)                
##  scales        1.0.0      2018-08-09 [1] CRAN (R 3.5.1)                
##  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.1)                
##  sf          * 0.7-3      2019-02-21 [1] CRAN (R 3.5.2)                
##  stringi       1.3.1      2019-02-13 [1] CRAN (R 3.5.2)                
##  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.5.2)                
##  testthat      2.0.1      2018-10-13 [1] CRAN (R 3.5.1)                
##  tibble      * 2.0.1      2019-01-12 [1] CRAN (R 3.5.2)                
##  tidyr       * 0.8.2      2018-10-28 [1] CRAN (R 3.5.1)                
##  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.5.1)                
##  tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.5.0)                
##  triebeard     0.3.0      2016-08-04 [1] CRAN (R 3.5.0)                
##  tweenr        1.0.1      2018-12-14 [1] CRAN (R 3.5.2)                
##  units         0.6-2      2018-12-05 [1] CRAN (R 3.5.1)                
##  urltools      1.7.2      2019-02-04 [1] CRAN (R 3.5.2)                
##  usethis       1.4.0      2018-08-14 [1] CRAN (R 3.5.1)                
##  utf8          1.1.4      2018-05-24 [1] CRAN (R 3.5.0)                
##  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.0)                
##  xfun          0.5        2019-02-20 [1] CRAN (R 3.5.2)                
##  xml2          1.2.0      2018-01-24 [1] CRAN (R 3.5.0)                
##  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.5.1)                
## 
## [1] /usr/lib/R/library

rhdx