In this post/tutorial, we will show how to use rhdx, dplyr, purrr, sf and gganimate to show the number of fatal incidents in the region. The rhdx package is not yet on CRAN, so you will need to use the remotes package to install it.

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

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

G5 Sahel: conflict-related fatalities totaled 2,832 in 2018, a 74% increase relative to 2017.

- On average, 63% of fatalities have been concentrated in Mali since 2012.
- Last year, fatalities were largely located in central Mali, the Liptako-Gourma region & the Lake Chad basin. pic.twitter.com/feRtcxsScb

— José Luengo-Cabrera (@J_LuengoCabrera) February 26, 2019

Our visualization will be done for 5 countries in the Sahel : Burkina Faso, Mali, Chad, Mauritania and Niger.

The goal is visualize the number of fatal events in the Sahel between 2012 and 2018 using an animated map. In order to do that, will get the administrative boundaries from HDX using the rhdx.

wca is a simple feature and we can manipulate is using sf and dplyr. The data 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 check our data by plotting it

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

Now that we have our basemap, the next step is to get the conflict data. One of main source for conflict data in the Sahel is ACLED. We can also get ACLED conflict data from HDX. We will use this time the rhdx::search_datasets function which supports SOLR to do more complex queries. In our case, we want conflict data from the ACLED organization in HDX and from the 5 countries (group in HDX/CKAN terminology)

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> 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
## 
## [[2]]
## <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
## 
## [[3]]
## <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
## 
## [[4]]
## <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
## 
## [[5]]
## <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
## 
## [[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> 3124457, 3124458, 3124454, 3124455, 3124456, 31…
## $ iso              <dbl> 562, 562, 562, 562, 562, 562, 562, 562, 562, 56…
## $ event_id_cnty    <chr> "NIR798", "NIR799", "NIR795", "NIR796", "NIR797…
## $ event_id_no_cnty <dbl> 798, 799, 795, 796, 797, 794, 793, 792, 791, 78…
## $ event_date       <date> 2019-02-22, 2019-02-22, 2019-02-20, 2019-02-20…
## $ year             <dbl> 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,…
## $ time_precision   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,…
## $ event_type       <chr> "Violence against civilians", "Violence against…
## $ actor1           <chr> "JNIM: Group for Support of Islam and Muslims a…
## $ assoc_actor_1    <chr> NA, NA, "Teachers (Niger); Labour Group (Niger)…
## $ inter1           <dbl> 2, 2, 6, 3, 2, 4, 3, 6, 2, 2, 6, 6, 3, 3, 3, 2,…
## $ actor2           <chr> "Civilians (Niger)", "Civilians (Niger)", NA, "…
## $ assoc_actor_2    <chr> "Military Forces of Niger (2011-)", "Fishermen …
## $ inter2           <dbl> 7, 7, 0, 7, 7, 4, 1, 0, 7, 1, 0, 0, 1, 1, 1, 7,…
## $ interaction      <dbl> 27, 27, 60, 37, 27, 44, 13, 60, 27, 12, 60, 60,…
## $ region           <chr> "Western Africa", "Western Africa", "Western Af…
## $ country          <chr> "Niger", "Niger", "Niger", "Niger", "Niger", "N…
## $ admin1           <chr> "Tillaberi", "Diffa", "Niamey", "Agadez", "Diff…
## $ admin2           <chr> "Tera", "Diffa", "Ville de Niamey", "Tchirozeri…
## $ admin3           <chr> "Tera", "Bosso", "Lamorde", "ZR Tchirozerine", …
## $ location         <chr> "Oueze Bangou", "Garin-Amadou", "Niamey", "Agad…
## $ latitude         <dbl> 14.7683, 13.7491, 13.5200, 16.9700, 13.5631, 13…
## $ longitude        <dbl> 0.5312, 13.3686, 2.1200, 7.9900, 12.8587, 12.48…
## $ geo_precision    <dbl> 1, 1, 3, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1,…
## $ source           <chr> "WhatsApp", "AFP; ActuNiger", "AFP; RFI; ANP (N…
## $ source_scale     <chr> "Other", "National-International", "National-In…
## $ notes            <chr> "On February 22, presumed JNIM and/or ISGS mili…
## $ fatalities       <dbl> 0, 4, 0, 0, 0, 1, 2, 0, 2, 19, 0, 0, 4, 0, 0, 0…
## $ timestamp        <dbl> 1551122598, 1551122598, 1551122598, 1551122598,…
## $ iso3             <chr> "NER", "NER", "NER", "NER", "NER", "NER", "NER"…

We have all the data we need for our analysis, we just need the 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 basemap (g5_ab), the conflict data (g5_acled_fatalities_loc) with gganimate to dynamically show 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 = "inferno") +
  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.

devtools::session_info()
## Warning in system("timedatectl", intern = TRUE): running command
## 'timedatectl' had status 1
## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.5.2 (2018-12-20)
##  os       Debian GNU/Linux 9 (stretch)
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       Etc/UTC                     
##  date     2019-02-27                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package     * version    date       lib source                      
##  assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.2)              
##  backports     1.1.3      2018-12-14 [1] CRAN (R 3.5.2)              
##  bindr         0.1.1      2018-03-13 [1] CRAN (R 3.5.2)              
##  bindrcpp    * 0.2.2      2018-03-29 [1] CRAN (R 3.5.2)              
##  broom         0.5.1      2018-12-05 [1] CRAN (R 3.5.2)              
##  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.2)              
##  class         7.3-14     2015-08-30 [2] 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.2)              
##  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.2)              
##  crul          0.7.0      2019-01-04 [1] CRAN (R 3.5.2)              
##  curl          3.3        2019-01-10 [1] CRAN (R 3.5.2)              
##  DBI           1.0.0      2018-05-02 [1] CRAN (R 3.5.2)              
##  desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.2)              
##  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.2)              
##  digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.2)              
##  dplyr       * 0.7.8      2018-11-10 [1] CRAN (R 3.5.2)              
##  e1071         1.7-0.1    2019-01-21 [1] CRAN (R 3.5.2)              
##  evaluate      0.12       2018-10-09 [1] CRAN (R 3.5.2)              
##  fansi         0.4.0      2018-10-05 [1] CRAN (R 3.5.2)              
##  farver        1.1.0      2018-11-20 [1] CRAN (R 3.5.2)              
##  forcats     * 0.3.0      2018-02-19 [1] CRAN (R 3.5.2)              
##  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.2)              
##  generics      0.0.2      2018-11-29 [1] CRAN (R 3.5.2)              
##  gganimate   * 1.0.0      2019-01-02 [1] CRAN (R 3.5.2)              
##  ggplot2     * 3.1.0      2018-10-25 [1] CRAN (R 3.5.2)              
##  gifski        0.8.6      2018-09-28 [1] CRAN (R 3.5.2)              
##  glue          1.3.0      2018-07-17 [1] CRAN (R 3.5.2)              
##  gtable        0.2.0      2016-02-26 [1] CRAN (R 3.5.2)              
##  haven         2.0.0      2018-11-22 [1] CRAN (R 3.5.2)              
##  hms           0.4.2      2018-03-10 [1] CRAN (R 3.5.2)              
##  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.5.2)              
##  httpcode      0.2.0      2016-11-14 [1] CRAN (R 3.5.2)              
##  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 [2] CRAN (R 3.5.2)              
##  lazyeval      0.2.1      2017-10-29 [1] CRAN (R 3.5.2)              
##  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.2)              
##  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.2)              
##  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.5.2)              
##  modelr        0.1.2      2018-05-11 [1] CRAN (R 3.5.2)              
##  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.5.2)              
##  nlme          3.1-137    2018-04-07 [2] CRAN (R 3.5.2)              
##  pillar        1.3.1      2018-12-15 [1] CRAN (R 3.5.2)              
##  pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.2)              
##  pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.5.2)              
##  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.2)              
##  plyr          1.8.4      2016-06-08 [1] CRAN (R 3.5.2)              
##  png           0.1-7      2013-12-03 [1] CRAN (R 3.5.2)              
##  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.2)              
##  processx      3.2.1      2018-12-05 [1] CRAN (R 3.5.2)              
##  progress      1.2.0      2018-06-14 [1] CRAN (R 3.5.2)              
##  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.3.0      2018-10-04 [1] CRAN (R 3.5.2)              
##  Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.2)              
##  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.5.2)              
##  readxl        1.2.0      2018-12-19 [1] CRAN (R 3.5.2)              
##  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.2)              
##  rhdx        * 0.0.1.9000 2019-02-27 [1] gitlab (dickoa/rhdx@b62b40c)
##  rhxl          0.1.0.9000 2019-02-27 [1] gitlab (dickoa/rhxl@4c325b7)
##  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.2)              
##  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.2)              
##  scales        1.0.0      2018-08-09 [1] CRAN (R 3.5.2)              
##  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.2)              
##  sf          * 0.7-2      2018-12-20 [1] CRAN (R 3.5.2)              
##  stringi       1.2.4      2018-07-20 [1] CRAN (R 3.5.2)              
##  stringr     * 1.3.1      2018-05-10 [1] CRAN (R 3.5.2)              
##  testthat      2.0.1      2018-10-13 [1] CRAN (R 3.5.2)              
##  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.2)              
##  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.5.2)              
##  tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.5.2)              
##  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-2      2018-12-05 [1] CRAN (R 3.5.2)              
##  urltools      1.7.1      2018-08-03 [1] CRAN (R 3.5.2)              
##  usethis       1.4.0      2018-08-14 [1] CRAN (R 3.5.2)              
##  utf8          1.1.4      2018-05-24 [1] CRAN (R 3.5.2)              
##  viridisLite   0.3.0      2018-02-01 [1] CRAN (R 3.5.2)              
##  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.2)              
##  xfun          0.4        2018-10-23 [1] CRAN (R 3.5.2)              
##  xml2          1.2.0      2018-01-24 [1] CRAN (R 3.5.2)              
##  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.5.2)              
## 
## [1] /usr/local/lib/R/site-library
## [2] /usr/local/lib/R/library