Ahmadou Dicko

10 minutes read


In this short post (crossposted here), 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

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)
## Rows: 24
## Columns: 6
## $ OBJECTID   <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ admin0Name <chr> "Benin", "Burkina Faso", "Cabo Verde", "Cameroon", "Centra…
## $ admin0Pcod <chr> "BJ", "BF", "CV", "CM", "CF", "TD", "CI", "CD", "GQ", "GA"…
## $ Shape_Leng <dbl> 19.006363, 31.466913, 13.599357, 47.469736, 49.366871, 56.…
## $ Shape_Area <dbl> 9.52157812, 22.72097221, 0.34276153, 38.08678102, 50.72939…
## $ geometry   <MULTIPOLYGON [°]> MULTIPOLYGON (((2.886863 12..., MULTIPOLYGON …

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/2020
##   Tags (up to 5): hxl, protests, security incidents, violence and conflict
##   Locations (up to 5): tcd
##   Resources (up to 5): Conflict Data for Chad, QuickCharts-Conflict Data for Chad
## 
## [[2]]
## <HDX Dataset> 537ee9a6-ff76-4e00-a9f6-1c7c16ae1628 
##   Title: Mauritania - Conflict Data
##   Name: acled-data-for-mauritania
##   Date: 01/01/1997-12/31/2020
##   Tags (up to 5): hxl, protests, security incidents, violence and conflict
##   Locations (up to 5): mrt
##   Resources (up to 5): Conflict Data for Mauritania, QuickCharts-Conflict Data for Mauritania
## 
## [[3]]
## <HDX Dataset> 5895de63-010c-4716-97cb-fbdd3caf4e3a 
##   Title: Mali - Conflict Data
##   Name: acled-data-for-mali
##   Date: 01/01/1997-12/31/2020
##   Tags (up to 5): hxl, protests, security incidents, violence and conflict
##   Locations (up to 5): mli
##   Resources (up to 5): Conflict Data for Mali, QuickCharts-Conflict Data for Mali
## 
## [[4]]
## <HDX Dataset> 653dc159-097d-4fef-9527-53ee30d132ff 
##   Title: Niger - Conflict Data
##   Name: acled-data-for-niger
##   Date: 01/01/1997-12/31/2020
##   Tags (up to 5): hxl, protests, security incidents, violence and conflict
##   Locations (up to 5): ner
##   Resources (up to 5): Conflict Data for Niger, QuickCharts-Conflict Data for Niger
## 
## [[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/2020
##   Tags (up to 5): hxl, protests, security incidents, violence and conflict
##   Locations (up to 5): bfa
##   Resources (up to 5): Conflict Data for Burkina Faso, QuickCharts-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): geodata, protests, violence and conflict, vulnerable populations
##   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)

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(longitude, 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.6.3 (2020-02-29)
##  os       Ubuntu 18.04.4 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language en_US:en                    
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       America/Panama              
##  date     2020-04-21                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version    date       lib source          
##  assertthat    0.2.1      2019-03-21 [3] CRAN (R 3.5.3)  
##  backports     1.1.6      2020-04-05 [3] CRAN (R 3.6.3)  
##  blogdown      0.18       2020-03-04 [1] CRAN (R 3.6.3)  
##  bookdown      0.18       2020-03-05 [1] CRAN (R 3.6.3)  
##  broom         0.5.5      2020-02-29 [1] CRAN (R 3.6.3)  
##  callr         3.4.3      2020-03-28 [3] CRAN (R 3.6.3)  
##  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.5.2)  
##  class         7.3-16     2020-03-25 [4] CRAN (R 3.6.3)  
##  classInt      0.4-3      2020-04-07 [1] CRAN (R 3.6.3)  
##  cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.3)  
##  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.5.3)  
##  crayon        1.3.4      2017-09-16 [3] CRAN (R 3.5.0)  
##  crul          0.9.0      2019-11-06 [1] CRAN (R 3.6.1)  
##  curl          4.3        2019-12-02 [1] CRAN (R 3.6.1)  
##  DBI           1.1.0      2019-12-15 [1] CRAN (R 3.6.2)  
##  dbplyr        1.4.3      2020-04-19 [1] CRAN (R 3.6.3)  
##  desc          1.2.0      2018-05-01 [3] CRAN (R 3.5.0)  
##  devtools      2.3.0      2020-04-10 [3] CRAN (R 3.6.3)  
##  digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.3)  
##  dplyr       * 0.8.5      2020-03-07 [1] CRAN (R 3.6.3)  
##  e1071         1.7-3      2019-11-26 [1] CRAN (R 3.6.1)  
##  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.1)  
##  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)  
##  fansi         0.4.1      2020-01-08 [3] CRAN (R 3.6.2)  
##  farver        2.0.3      2020-01-16 [1] CRAN (R 3.6.3)  
##  forcats     * 0.5.0      2020-03-01 [1] CRAN (R 3.6.3)  
##  fs            1.4.1      2020-04-04 [3] CRAN (R 3.6.3)  
##  generics      0.0.2      2018-11-29 [1] CRAN (R 3.5.2)  
##  gganimate   * 1.0.5      2020-02-09 [1] CRAN (R 3.6.3)  
##  ggplot2     * 3.2.1      2020-03-30 [1] local           
##  gifski        0.8.6      2018-09-28 [1] CRAN (R 3.6.1)  
##  glue          1.4.0      2020-04-03 [3] CRAN (R 3.6.3)  
##  gtable        0.3.0      2019-03-25 [3] CRAN (R 3.5.3)  
##  haven         2.2.0      2019-11-08 [1] CRAN (R 3.6.1)  
##  hms           0.5.3      2020-01-08 [1] CRAN (R 3.6.2)  
##  hoardr        0.5.2      2018-12-02 [1] CRAN (R 3.6.1)  
##  htmltools     0.4.0      2019-10-04 [3] CRAN (R 3.6.1)  
##  httpcode      0.3.0      2020-04-10 [1] CRAN (R 3.6.3)  
##  httr          1.4.1      2019-08-05 [3] CRAN (R 3.6.1)  
##  jsonlite      1.6.1      2020-02-02 [3] CRAN (R 3.6.2)  
##  KernSmooth    2.23-16    2019-10-15 [4] CRAN (R 3.6.1)  
##  knitr         1.28       2020-02-06 [3] CRAN (R 3.6.2)  
##  lattice       0.20-41    2020-04-02 [4] CRAN (R 3.6.3)  
##  lazyeval      0.2.2      2019-03-15 [3] CRAN (R 3.5.3)  
##  lifecycle     0.2.0      2020-03-06 [1] CRAN (R 3.6.3)  
##  lubridate     1.7.8      2020-04-06 [1] CRAN (R 3.6.3)  
##  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.2)  
##  memoise       1.1.0      2017-04-21 [3] CRAN (R 3.5.0)  
##  modelr        0.1.6      2020-02-22 [1] CRAN (R 3.6.3)  
##  munsell       0.5.0      2018-06-12 [3] CRAN (R 3.5.0)  
##  nlme          3.1-145    2020-03-04 [4] CRAN (R 3.6.3)  
##  pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.2)  
##  pkgbuild      1.0.6      2019-10-09 [3] CRAN (R 3.6.1)  
##  pkgconfig     2.0.3      2019-09-22 [3] CRAN (R 3.6.1)  
##  pkgload       1.0.2      2018-10-29 [3] CRAN (R 3.5.1)  
##  prettyunits   1.1.1      2020-01-24 [3] CRAN (R 3.6.2)  
##  processx      3.4.2      2020-02-09 [1] CRAN (R 3.6.3)  
##  progress      1.2.2      2019-05-16 [1] CRAN (R 3.6.0)  
##  ps            1.3.2      2020-02-13 [3] CRAN (R 3.6.2)  
##  purrr       * 0.3.4      2020-04-17 [1] CRAN (R 3.6.3)  
##  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.1)  
##  rappdirs      0.3.1      2016-03-28 [1] CRAN (R 3.6.0)  
##  Rcpp          1.0.4.6    2020-04-09 [1] CRAN (R 3.6.3)  
##  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.5.2)  
##  readxl        1.3.1      2019-03-13 [1] CRAN (R 3.5.3)  
##  remotes       2.1.1      2020-02-15 [3] CRAN (R 3.6.2)  
##  reprex        0.3.0      2019-05-16 [1] CRAN (R 3.6.0)  
##  rhdx        * 0.1.0.9000 2019-12-10 [1] git2r (@cc14cb0)
##  rlang         0.4.5      2020-03-01 [1] CRAN (R 3.6.3)  
##  rmarkdown     2.1        2020-01-20 [1] CRAN (R 3.6.3)  
##  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.2)  
##  rstudioapi    0.11       2020-02-07 [3] CRAN (R 3.6.2)  
##  rvest         0.3.5      2019-11-08 [1] CRAN (R 3.6.1)  
##  scales        1.1.0      2019-11-18 [3] CRAN (R 3.6.1)  
##  sessioninfo   1.1.1      2018-11-05 [3] CRAN (R 3.5.1)  
##  sf          * 0.9-2      2020-04-14 [1] CRAN (R 3.6.3)  
##  stringi       1.4.6      2020-02-17 [1] CRAN (R 3.6.3)  
##  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.5.2)  
##  testthat      2.3.2      2020-03-02 [1] CRAN (R 3.6.3)  
##  tibble      * 3.0.1      2020-04-20 [1] CRAN (R 3.6.3)  
##  tidyr       * 1.0.2      2020-01-24 [1] CRAN (R 3.6.3)  
##  tidyselect    1.0.0      2020-01-27 [1] CRAN (R 3.6.3)  
##  tidyverse   * 1.3.0      2019-11-21 [1] CRAN (R 3.6.1)  
##  triebeard     0.3.0      2016-08-04 [1] CRAN (R 3.5.2)  
##  tweenr        1.0.1      2018-12-14 [1] CRAN (R 3.5.2)  
##  units         0.6-6      2020-03-16 [1] CRAN (R 3.6.3)  
##  urltools      1.7.3      2019-04-14 [1] CRAN (R 3.6.0)  
##  usethis       1.6.0      2020-04-09 [3] CRAN (R 3.6.3)  
##  utf8          1.1.4      2018-05-24 [3] CRAN (R 3.5.0)  
##  vctrs         0.2.4      2020-03-10 [1] CRAN (R 3.6.3)  
##  withr         2.1.2      2018-03-15 [3] CRAN (R 3.5.0)  
##  xfun          0.13       2020-04-13 [1] CRAN (R 3.6.3)  
##  xml2          1.3.1      2020-04-09 [1] CRAN (R 3.6.3)  
##  yaml          2.2.1      2020-02-01 [3] CRAN (R 3.6.2)  
## 
## [1] /home/edouard/R/x86_64-pc-linux-gnu-library/3.6
## [2] /usr/local/lib/R/site-library
## [3] /usr/lib/R/site-library
## [4] /usr/lib/R/library
comments powered by Disqus