library(stringr)

# Get the current page number from the file name
current_page <- as.numeric(str_extract(knitr::current_input(), "\\d+"))

# Set the total number of pages
total_pages <- 9

# Generate the URLs for the previous and next pages
previous_page <- ifelse(current_page > 1, paste0("visual_", current_page - 1, "-darfur_violence-code_included.html"), NA)
next_page <- ifelse(current_page < total_pages, paste0("visual_", current_page + 1, "-darfur_violence-code_included.html"), NA)


# Note: All code, insights shared, and methodological notes in this document and other documents in this portfolio ARE NOT open source and MUST be cited.

library(tidyverse)
library(ggplot2)
library(ggraph)
library(ggmap)
library(ggpubr)
library(readxl)

setwd("C:/Users/rsb84/Desktop/RB/COLUMBIA/QMSS/COURSES/Spring_2021/Data Visualization/End_project")


ACLED_data <- readxl::read_excel("ACLED-DARFUR-VAC-2008-2021 (After Course Ended-for 2023 Portfolio)-UPDATED VERSION-inter1_numbers_replaced_with_actor_names.xlsx", 
     col_types = c("date", "numeric", "text", 
         "text", "text", "text", "text", "text", 
         "text", "text", "text", "text", "text", 
         "numeric", "numeric", "text", "text", 
         "text", "numeric", "numeric"))

acled_short=ACLED_data[c("event_date","year","sub_event_type","actor1","inter1","admin1","admin2","location","latitude","longitude","attacks","fatalities")]


# Below, I have created faceted heat density maps which allow for comparisons of the locations where violent attacks on civilians were most concentrated between 2016 and 2021 across Darfur, along with depictions of the locations of both operational and closed UN bases

acled_2016 = subset(acled_short, year == 2016)
acled_2017 = subset(acled_short, year == 2017)
acled_2018 = subset(acled_short, year == 2018)
acled_2019 = subset(acled_short, year == 2019)
acled_2020 = subset(acled_short, year == 2020)
acled_2021 = subset(acled_short, year == 2021)


# Create columns that show which bases existed during which years

# 2016:

geo_pko <- readxl::read_excel("active_bases-removed_bases_gone_more_than_6_months-Geo-PKO-v-2-1-2023_portfolio.xlsx")

geo_pko_for_maps_16 = filter(geo_pko, year == 2016)
geo_pko_df_16 = geo_pko_for_maps_16[c("year","location","latitude", "longitude")]
geo_pko_16_avg_troops_per_base = geo_pko_for_maps_16 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_16 = inner_join(geo_pko_16_avg_troops_per_base, geo_pko_df_16, by="location")
geo_pko_df_16 = distinct(geo_pko_df_16)
active_bases_2016 = geo_pko_df_16



# 2017

geo_pko_for_maps_17 = filter(geo_pko, year == 2017)
geo_pko_df_17 = geo_pko_for_maps_17[c("year","location","latitude", "longitude")]
geo_pko_17_avg_troops_per_base = geo_pko_for_maps_17 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_17 = inner_join(geo_pko_17_avg_troops_per_base, geo_pko_df_17, by="location")
geo_pko_df_17 = distinct(geo_pko_df_17)
active_bases_2017 = geo_pko_df_17

# 2018

geo_pko_for_maps_18 = filter(geo_pko, year == 2018)
geo_pko_df_18 = geo_pko_for_maps_18[c("year","location","latitude", "longitude")]
geo_pko_18_avg_troops_per_base = geo_pko_for_maps_18 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_18 = inner_join(geo_pko_18_avg_troops_per_base, geo_pko_df_18, by="location")
geo_pko_df_18 = distinct(geo_pko_df_18)
active_bases_2018 = geo_pko_df_18

# 2019

geo_pko_for_maps_19 = filter(geo_pko, year == 2019)
geo_pko_df_19 = geo_pko_for_maps_19[c("year","location","latitude", "longitude")]
geo_pko_19_avg_troops_per_base = geo_pko_for_maps_19 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_19 = inner_join(geo_pko_19_avg_troops_per_base, geo_pko_df_19, by="location")
geo_pko_df_19 = distinct(geo_pko_df_19)
active_bases_2019 = geo_pko_df_19


# 2020

geo_pko_for_maps_20 = filter(geo_pko, year == 2020)
geo_pko_df_20 = geo_pko_for_maps_20[c("year","location","latitude", "longitude")]
geo_pko_20_avg_troops_per_base = geo_pko_for_maps_20 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_20 = inner_join(geo_pko_20_avg_troops_per_base, geo_pko_df_20, by="location")
geo_pko_df_20 = distinct(geo_pko_df_20)
active_bases_2020 = geo_pko_df_20

# 2021

geo_pko_for_maps_21 = filter(geo_pko, year == 2021)
geo_pko_df_21 = geo_pko_for_maps_21[c("year","location","latitude", "longitude")]
geo_pko_21_avg_troops_per_base = geo_pko_for_maps_21 %>% group_by(location) %>% summarise(avg.troops = mean(no.troops, na.rm=T))
geo_pko_df_21 = inner_join(geo_pko_21_avg_troops_per_base, geo_pko_df_21, by="location")
geo_pko_df_21 = distinct(geo_pko_df_21)
active_bases_2021 = geo_pko_df_21


# Note: In my UN "closed bases" files, I do not include sites that were active in July or after of the given year but later closed in that same year since July is the 7th month of the year - meaning such bases were active for more than half of the given year.


# Sources for the dates of UN base closures:
# https://oios.un.org/file/8866/download?token=F6tc9ZI5 (pg. 31) - documents sites closed from 2017-2019 (I consider "team sites" and "section headquarters" as UN bases since troops are normally stationed at these types of compounds)
# https://www.ecoi.net/en/file/local/2052225/S_2021_470_E.pdf (pg. 24) - documents sites closed in 2021

# Note: No UN bases were closed in 2020, but were closed in 2021.

# Note: Each of the closed base csv files build on each other. E.g., the closed bases file for 2018 includes UN bases that closed in 2017 and 2018. The closed base file for 2019 includes bases that closed in 2017, 2018, and 2019, etc.


closed_bases_2017 <- read_csv("closed_bases_2017-2023_portfolio.csv")

closed_bases_2018 <- read_csv("closed_bases_2018-2023_portfolio.csv")

closed_bases_2019 <- read_csv("closed_bases_2019-2023_portfolio.csv")

closed_bases_2020 <- read_csv("closed_bases_2020-2023_portfolio.csv")

closed_bases_2021 <- read_csv("closed_bases_2021-2023_portfolio.csv")
# MAPS FROM 2016-2021, FACETED

# You must register a free API key first on https://client.stadiamaps.com/ Once registering your username and password to be able to login to the site, navigate to the "Dashboard". Then go to "Manage Properties". Under "Authentication Configuration", you can generate and view your API key. Once you have your Stadia Maps API key, first use ggmap::register_stadiamaps("YOUR-API-KEY", write=TRUE) to save the API key to your .Renviron file. Then you can proceed with using the following code:

map_darfur <- get_stadiamap(bbox = c(21.71368, 8.64132, 27.58313, 16.12054), zoom = 7, maptype = 'stamen_terrain')


map_2016 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) + #16.217571
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) + #22.6667, 13.4167 or 14.1
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2016, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2016, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=22, color="steelblue") +
guides(size="none")  + 
theme(legend.position="bottom", title =element_text(size=13, color=  "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2016", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))




map_2017 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) + 
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) + #23.116657
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2017, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2017, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=22, color="steelblue") + 
geom_point(data = closed_bases_2017, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=25, color="darkgreen") +
guides(size="none") +
guides(size="none")  + 
theme(legend.position="none", title =element_text(size=13, color=  "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2017", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))




map_2018 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) + 
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) +
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2018, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2018, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.5), shape=22, color="steelblue") +
guides(size="none")  + 
geom_point(data = closed_bases_2018, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=25, color="darkgreen") +
guides(size="none")  +
theme(legend.position="none", title =element_text(size=13, color= "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2018", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))




map_2019 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) + 
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) +
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2019, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2019, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.5), shape=22, color="steelblue") +
guides(size="none")  + 
geom_point(data = closed_bases_2019, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=25, color="darkgreen") +
guides(size="none")  +
theme(legend.position="none", title =element_text(size=13, color= "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2019", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))




map_2020 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) +
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) +
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2020, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2020, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.5), shape=22, color="steelblue") +
guides(size="none")  + 
geom_point(data = closed_bases_2020, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=25, color="darkgreen") +
guides(size="none")  +
theme(legend.position="none", title =element_text(size=13, color=  "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2020", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))




map_2021 = ggmap(map_darfur) + 
scale_fill_gradient(low="yellow", high="red") + 
annotate("text", x= 26.609638, y= 10.817195, label="East Darfur", fontface=2,size=3) + 
annotate("text", x= 25.0, y= 14.817571, label="North Darfur", fontface=2,size=3) + 
annotate("text", x= 24.420469, y= 11.002820, label="South Darfur", fontface=2,size=3) + 
annotate("text", x= 22.8333285, y= 13.670825, label="West Darfur", fontface=2,size=3) +
annotate("text", x= 23.3, y= 12.3833, label="Central Darfur", fontface=2,size=3) + 
stat_density2d(data=acled_2021, geom = "polygon", aes(x=longitude, y=latitude,fill=..level.., alpha=..level..)) + 
guides(alpha="none") + 
geom_point(data = active_bases_2021, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.5), shape=22, color="steelblue") +
guides(size="none")  + 
geom_point(data = closed_bases_2021, mapping = aes(x = longitude, y = latitude, size=1.5, alpha=0.3), shape=25, color="darkgreen") +
guides(size="none")  +
theme(legend.position="none", title =element_text(size=13, color=  "steelblue",face='bold'), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), rect = element_blank()) + 
labs(title="2021", subtitle="", caption = "") + 
guides(fill=guide_legend(title="Concentration of All\nAttacks on Civilians"))


It can be seen that attacks on civilians from 2016 to 2018 were primarily concentrated in the region where the borders of Central, North, and South Darfur meet. However, the concentration of attacks substantially condensed into the southern part of North Darfur in 2019 - the year in which the UN formally decided to withdraw the entire mission - before suddenly expanding in 2020 mostly into the northeastern part of Central Darfur and also into West Darfur. The expansion into West Darfur is particularly noteable since a concentration of attacks there only started emerging in 2019.


In 2021, while the highest density of attacks on civilians shifted to the south-central part of North Darfur, the concentration of attacks in West Darfur still grew stronger.


map_facet <- ggarrange(map_2016, map_2017, map_2018, map_2019, map_2020, map_2021, heights = c(.8, .8, .8, .8, .8, .8), widths = c(.8,.8,.8,.8,.8,.8), align = "h", nrow = 2, ncol=3, common.legend = TRUE, legend="bottom")


annotate_figure(map_facet, top = text_grob("Concentration of All Attacks on Darfur Civilians, 2016-2021", face = "bold", size = 26, color="steelblue", vjust = 0.5), bottom = text_grob("Note: Squares are Active UN Bases. Triangles are Closed Bases", size = 12, color ="steelblue"))


It is possible this expansion of the density of attacks into West Darfur may partly result from the UN closing 5 of its bases in the state from 2017-2019. Potential causes of this change in density deserve more inspection. In Visual 3, we saw an unusually large spike in civilian killings in Western Darfur (46) in 2019, and we saw that in 2021 there were more civilian killings there (155) than in any other state in Darfur. Although neither the 2019 nor the 2021 density maps in this visual show the highest concentration of attacks on civilians being in West Darfur, what may account for the discrepancy is that there was likely a larger than normal number of fatalities per attack in 2019 and 2021. We will investigate this further in Visual 7.


In the next visual, I will create a faceted choropleth map of Darfur comparing the years 2016 and 2020, with bubble size representing the number of fatalities per attack on civilians, and with varying shades of green in second order administrative districts reflecting the average number of UN troops present. While it would have been more ideal to map 2021 instead of 2020 in Visual 6 to have a proper before vs. after base withdrawal comparison, recall that the Geo-PKO dataset which I rely upon for UN troop figures only extends to 2020.


# Get the current page number from the file name
current_page <- as.numeric(str_extract(knitr::current_input(), "\\d+"))

# Set the total number of pages
total_pages <- 9

# Generate the URLs for the previous and next pages
previous_page <- ifelse(current_page > 1, paste0("visual_", current_page - 1, "-darfur_violence-code_included.html"), NA)
next_page <- ifelse(current_page < total_pages, paste0("visual_", current_page + 1, "-darfur_violence-code_included.html"), NA)