remotes::install_gitlab("dickoa/rhdx") ## github mirror also avalailableIn this short post, we will show how to use the rhdx, racled, 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:
The package racled will be used to pull conflict data from the ACLED project. ACLED is a NGO specialized in conflict data collection and analysis.
remotes::install_gitlab("dickoa/racled") ## github mirror also avalailablegganimate 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.
G5 Sahel: conflict-related fatalities totaled 2,832 in 2018, a 74% increase relative to 2017.
— José Luengo-Cabrera (@J_LuengoCabrera) February 26, 2019
- 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
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(racled)
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.
wca <- pull_dataset("west-and-central-africa-administrative-boundaries-levels") %>%
get_resource(1) %>%
read_resource()
glimpse(wca)Rows: 24
Columns: 6
$ admin0Name <chr> "Benin", "Burkina Faso", "Cabo Verde", "Came…
$ admin0Pcod <chr> "BJ", "BF", "CV", "CM", "CF", "TD", "CI", "C…
$ last_modif <date> 2021-06-22, 2021-06-22, 2021-06-22, 1899-12…
$ source <chr> "OCHAfrom ctrylayers", "OCHAfrom ctrylayers"…
$ date <date> 2021-06-24, 2021-06-24, 2021-06-24, 2021-06…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((1.863432 6....,…
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 using the racled package.
g5_acled_data <- read_acled(c("mali", "mauritania", "niger", "chad", "burkina faso"))
glimpse(g5_acled_data)Rows: 21,270
Columns: 31
$ data_id <int> 9702717, 9702716, 9702764, 9702768, 97…
$ iso <int> 466, 466, 466, 466, 466, 466, 466, 466…
$ event_id_cnty <chr> "MLI7810", "MLI7809", "MLI7857", "MLI7…
$ event_id_no_cnty <int> 7810, 7809, 7857, 7861, 7862, 7801, 78…
$ event_date <chr> "2022-12-09", "2022-12-08", "2022-12-0…
$ year <int> 2022, 2022, 2022, 2022, 2022, 2022, 20…
$ time_precision <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ event_type <chr> "Violence against civilians", "Battles…
$ sub_event_type <chr> "Attack", "Armed clash", "Attack", "Lo…
$ actor1 <chr> "Military Forces of Mali (2021-)", "JN…
$ assoc_actor_1 <chr> "Wagner Group", "", "", "Dan Na Ambass…
$ inter1 <int> 1, 2, 3, 8, 8, 2, 2, 8, 3, 3, 2, 4, 2,…
$ actor2 <chr> "Civilians (Mali)", "Police Forces of …
$ assoc_actor_2 <chr> "Labour Group (Mali); JNIM: Group for …
$ inter2 <int> 7, 1, 7, 7, 7, 7, 7, 2, 7, 7, 2, 7, 7,…
$ interaction <int> 17, 12, 37, 78, 78, 27, 27, 28, 37, 37…
$ region <chr> "Western Africa", "Western Africa", "W…
$ country <chr> "Mali", "Mali", "Mali", "Mali", "Mali"…
$ admin1 <chr> "Mopti", "Segou", "Gao", "Mopti", "Mop…
$ admin2 <chr> "Douentza", "Segou", "Ansongo", "Djenn…
$ admin3 <chr> "Gandamia", "Cinzana", "Bara", "Femaye…
$ location <chr> "Kikara", "Cinzana Gare", "Tabango", "…
$ latitude <dbl> 15.2107, 13.2520, 15.8057, 13.9819, 15…
$ longitude <dbl> -2.7489, -5.9656, 0.3672, -4.2883, -2.…
$ geo_precision <int> 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2,…
$ source <chr> "Whatsapp", "Facebook; Undisclosed Sou…
$ source_scale <chr> "New media", "Local partner-Other", "N…
$ notes <chr> "On 9 December 2022, FAMa and Wagner f…
$ fatalities <int> 2, 2, 0, 0, 3, 0, 2, 0, 0, 2, 40, 0, 0…
$ timestamp <int> 1670870899, 1670870899, 1670870899, 16…
$ iso3 <chr> "MLI", "MLI", "MLI", "MLI", "MLI", "ML…
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 4.2.2 Patched (2022-11-12 r83340)
os Arch Linux
system x86_64, linux-gnu
ui X11
language en_US.UTF-8
collate en_US.UTF-8
ctype en_US.UTF-8
tz UTC
date 2022-12-28
pandoc 2.19.2 @ /usr/bin/ (via rmarkdown)
─ Packages ────────────────────────────────────────────────────
package * version date (UTC) lib source
abind 1.4-5 2016-07-21 [1] CRAN (R 4.2.2)
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.2)
backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.2)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.2.2)
broom 1.0.2 2022-12-15 [1] CRAN (R 4.2.2)
cachem 1.0.6 2021-08-19 [1] CRAN (R 4.2.2)
callr 3.7.3 2022-11-02 [1] CRAN (R 4.2.2)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.2.2)
class 7.3-20 2022-01-16 [1] CRAN (R 4.2.2)
classInt 0.4-8 2022-09-29 [1] CRAN (R 4.2.2)
cli 3.5.0 2022-12-20 [1] CRAN (R 4.2.2)
colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.2)
countrycode 1.4.0 2022-05-04 [1] CRAN (R 4.2.2)
crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.2)
crul 1.3 2022-09-03 [1] CRAN (R 4.2.2)
curl 4.3.3 2022-10-06 [1] CRAN (R 4.2.2)
DBI 1.1.3 2022-06-18 [1] CRAN (R 4.2.2)
dbplyr 2.2.1 2022-06-27 [1] CRAN (R 4.2.2)
devtools 2.4.5 2022-10-11 [1] CRAN (R 4.2.2)
digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2)
dplyr * 1.0.10 2022-09-01 [1] CRAN (R 4.2.2)
e1071 1.7-12 2022-10-24 [1] CRAN (R 4.2.2)
ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.2)
evaluate 0.19 2022-12-13 [1] CRAN (R 4.2.2)
fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.2)
farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.2)
fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.2)
forcats * 0.5.2 2022-08-19 [1] CRAN (R 4.2.2)
fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.2)
gargle 1.2.1 2022-09-08 [1] CRAN (R 4.2.2)
generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.2)
gganimate * 1.0.8.9000 2022-11-22 [1] Github (thomasp85/gganimate@7cd46dc)
ggplot2 * 3.4.0 2022-11-04 [1] CRAN (R 4.2.2)
gifski 1.6.6-1 2022-11-22 [1] Github (r-rust/gifski@68911d9)
glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.2)
googledrive 2.0.0 2021-07-08 [1] CRAN (R 4.2.2)
googlesheets4 1.0.1 2022-08-13 [1] CRAN (R 4.2.2)
gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.2)
haven 2.5.1 2022-08-22 [1] CRAN (R 4.2.2)
hms 1.1.2 2022-08-19 [1] CRAN (R 4.2.2)
hoardr 0.5.2 2018-12-02 [1] CRAN (R 4.2.2)
htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.2)
htmlwidgets 1.6.0 2022-12-15 [1] CRAN (R 4.2.2)
httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.2.2)
httpuv 1.6.7 2022-12-14 [1] CRAN (R 4.2.2)
httr 1.4.4 2022-08-17 [1] CRAN (R 4.2.2)
jsonlite 1.8.4 2022-12-06 [1] CRAN (R 4.2.2)
KernSmooth 2.23-20 2021-05-03 [1] CRAN (R 4.2.2)
knitr 1.41 2022-11-18 [1] CRAN (R 4.2.2)
later 1.3.0 2021-08-18 [1] CRAN (R 4.2.2)
lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.2)
lubridate 1.9.0 2022-11-06 [1] CRAN (R 4.2.2)
lwgeom 0.2-10 2022-11-19 [1] CRAN (R 4.2.2)
magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.2)
memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.2)
mime 0.12 2021-09-28 [1] CRAN (R 4.2.2)
miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.2.2)
modelr 0.1.10 2022-11-11 [1] CRAN (R 4.2.2)
munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.2)
pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.2)
pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.2.2)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.2)
pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.2.2)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.2)
processx 3.8.0 2022-10-26 [1] CRAN (R 4.2.2)
profvis 0.3.7 2020-11-02 [1] CRAN (R 4.2.2)
progress 1.2.2 2019-05-16 [1] CRAN (R 4.2.2)
promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.2.2)
proxy 0.4-27 2022-06-09 [1] CRAN (R 4.2.2)
ps 1.7.2 2022-10-26 [1] CRAN (R 4.2.2)
purrr * 1.0.0 2022-12-20 [1] CRAN (R 4.2.2)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.2)
racled * 0.0.0.9000 2022-11-03 [1] gitlab (dickoa/racled@82f016a)
rappdirs 0.3.3 2021-01-31 [1] CRAN (R 4.2.2)
Rcpp 1.0.9 2022-07-08 [1] CRAN (R 4.2.2)
readr * 2.1.3 2022-10-01 [1] CRAN (R 4.2.2)
readxl 1.4.1 2022-08-17 [1] CRAN (R 4.2.2)
remotes 2.4.2 2021-11-30 [1] CRAN (R 4.2.2)
reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.2)
rhdx * 0.1.0.9000 2022-11-03 [1] gitlab (dickoa/rhdx@c443336)
rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.2)
rmarkdown 2.19 2022-12-15 [1] CRAN (R 4.2.2)
rvest 1.0.3 2022-08-19 [1] CRAN (R 4.2.2)
scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.2)
sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.2)
sf * 1.0-9 2022-11-08 [1] CRAN (R 4.2.2)
shiny 1.7.4 2022-12-15 [1] CRAN (R 4.2.2)
stars 0.6-0 2022-11-21 [1] CRAN (R 4.2.2)
stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.2)
stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.2.2)
tibble * 3.1.8 2022-07-22 [1] CRAN (R 4.2.2)
tidyr * 1.2.1 2022-09-08 [1] CRAN (R 4.2.2)
tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.2)
tidyverse * 1.3.2 2022-07-18 [1] CRAN (R 4.2.2)
timechange 0.1.1 2022-11-04 [1] CRAN (R 4.2.2)
triebeard 0.3.0 2016-08-04 [1] CRAN (R 4.2.2)
tweenr 2.0.2 2022-09-06 [1] CRAN (R 4.2.2)
tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.2.2)
units 0.8-1 2022-12-10 [1] CRAN (R 4.2.2)
urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.2.2)
urltools 1.7.3 2019-04-14 [1] CRAN (R 4.2.2)
usethis 2.1.6 2022-05-25 [1] CRAN (R 4.2.2)
utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.2)
vctrs 0.5.1 2022-11-16 [1] CRAN (R 4.2.2)
withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.2)
xfun 0.36 2022-12-21 [1] CRAN (R 4.2.2)
xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.2)
xtable 1.8-4 2019-04-21 [1] CRAN (R 4.2.2)
yaml 2.3.6 2022-10-18 [1] CRAN (R 4.2.2)
[1] /usr/lib/R/library
───────────────────────────────────────────────────────────────