The interactive plot below examines South Darfur and West Darfur
with respect to the proportion of all civilian killings carried out by
each type of perpetrator.
# 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(ggthemes)
library(plotly)
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-LATEST-inter1_integers_version.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")]
acled_2016_2021_west_south = subset(acled_short, acled_short$year != 2008 & acled_short$year != 2009 & acled_short$year != 2010 & acled_short$year != 2011 & acled_short$year != 2012 & acled_short$year != 2013 & acled_short$year != 2014 & acled_short$year != 2015 & acled_short$admin1 != 'North Darfur' & acled_short$admin1 != 'East Darfur' & acled_short$admin1 != 'Central Darfur')
fatalities_by_region_year_actor=acled_2016_2021_west_south %>% group_by(admin1, year, inter1) %>% tally(fatalities)
# The ACLED dataset lists the values of the column inter1 as being between 1 and 8. These values have a specific meaning. 1 corresponds to "State Forces", 2 to "Rebel Groups", 3 to "Political Militias", 4 to "Identity Militias", 5 to "Rioters", 6 to "Protesters", 7 to "Civilians", and 8 to "External/Other Forces". However, I inspected the dataframe acled_2016_2021_west_south from which the grouped dataframe fatalities_by_region_year_actor was created, and found there were no values for the column inter1 between 4 and 8. I.e., only "State Forces", "Political Militias", "Identity Militias", "Rebel Groups", and "External/Other Forces" were responsible for attacks on civilians in the aforementioned dataframes. The code below ensures that these labels for inter1 will appear in the following order on legends of any tables and/or plots: "State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"
df2 <- fatalities_by_region_year_actor %>%
mutate(pct = prop.table(n) * 100) %>%
mutate(inter1 = factor(inter1, levels = c(1,3,4,2,8), labels = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"))) %>% #This controls the order of the armed groups on the legend
mutate(Percent = round(pct, digits = 1),
Perpetrator = inter1,
Year = year)
# Note: If the above string values were already in the column inter1 in the original excel file in place of integer values, the code to produce exactly the same order as the order above for legends of any tables or plots based on the grouped dataframe df2 would have been the following instead:
# df2 <- fatalities_by_region_year_actor %>%
# mutate(pct = prop.table(n) * 100) %>%
# mutate(inter1 = factor(inter1, levels = c(""State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"))) %>% #This controls the order of the armed groups on the legend
# mutate(Percent = round(pct, digits = 1),
# Perpetrator = inter1,
# Year = year)
# Define colors corresponding to each group
colors <- c("State Forces" = "maroon1",
"Political Militias" = "deepskyblue1",
"Identity Militias" = "peru",
"Rebel Groups" = "gray65",
"External/Other Forces" = "black")
# Specify the custom labels for the y-axis
custom_labels <- c("0", "25", "50", "75", "100")
# Create the plot
gg <- ggplot(df2, aes(x = Year, y = Percent, fill = Perpetrator)) +
geom_col(position = "fill", width = 0.8, size = 1, alpha = 0.7) +
scale_fill_manual(values = colors,
breaks = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"),
labels = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces")) +
scale_y_continuous(breaks = seq(0, 1, by = 0.25), labels = custom_labels) + # Set custom breaks and labels for y-axis
scale_x_continuous(breaks = seq(min(df2$Year), max(df2$Year), by = 1)) + # Ensure years are displayed without gaps
theme_wsj() +
theme(legend.position = "top",
title = element_text(size = 16, color = "steelblue", face = 'bold'),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 14),
strip.text.x = element_text(size=16, face = 'bold'),
legend.text=element_text(size=13)) +
labs(title = "Perpetrators' Share of Civilian Killings (2016-2021)",
subtitle = "Jan. 2016 - Dec. 2021",
caption = "") +
guides(fill = guide_legend(title = "")) +
facet_wrap(~ admin1)
ggplotly(gg, height = 600, width = 850)
The second interactive table displays the annual number of civilian
killings by perpetrator type, not their proportion. It shows that until
2019, South Darfur had more fatalities from identity militias than West
Darfur. However, this trend shifted in 2019 with 39 identity militia
killings (up from 15 the previous year) in West Darfur, and dramatically
so in 2021 with 144 such killings. The table also indicates a
significant decrease in the overall number of killings by state forces,
especially in South Darfur after 2018.
#Delete when done
filtered_data_for_inspection <- acled_2016_2021_west_south %>%
filter(
(admin1 == "South Darfur" & (year == 2018 | year == 2019)) |
(admin1 == "West Darfur" & (year == 2019 | year == 2020))
) %>%
mutate(Perpetrator = factor(inter1,
levels = c(1, 2, 3, 4, 5, 6, 7, 8),
labels = c("State Forces", "Rebel Groups", "Political Militias", "Identity Militias", "Rioters", "Protesters", "Civilians", "External/Other Forces"))) %>%
select(-inter1) %>%
filter(Perpetrator == "Political Militias") %>%
mutate(Year = year,
Actor = actor1) %>%
group_by(admin1, Year, Perpetrator, Actor) %>% # Group by admin1, Year, Perpetrator, and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
group_by(admin1, Year, Perpetrator, Actor) %>% # Regroup by
arrange(desc(Fatalities), .by_group = TRUE)
df3 <- fatalities_by_region_year_actor %>%
mutate(Fatalities = n) %>%
mutate(Perpetrator = factor(inter1, levels = c(1,3,4,2,8), labels = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"))) %>%
mutate(Year = year)
# Define colors corresponding to each group
colors <- c("State Forces" = "maroon1",
"Political Militias" = "deepskyblue1",
"Identity Militias" = "peru",
"Rebel Groups" = "gray65",
"External/Other Forces" = "black")
# Specify the custom labels for the y-axis
#custom_labels <- c("0", "25", "50", "75", "100")
# Create the plot
gg2 <- ggplot(df3, aes(x = Year, y = Fatalities, fill = Perpetrator)) +
geom_col(position = "stack", width = 0.8, size = 1, alpha = 0.7) +
scale_fill_manual(values = colors,
breaks = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"),
labels = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces")) +
# scale_y_continuous(breaks = seq(0, 1, by = 0.25), labels = custom_labels) + # Set custom breaks and labels for y-axis
scale_y_continuous(breaks = seq(min(df3$n), max(df3$n), by = 15)) +
scale_x_continuous(breaks = seq(min(df3$year), max(df3$year), by = 1)) + # Ensure years are displayed without gaps
theme_wsj() +
theme(legend.position = "top",
title = element_text(size = 16, color = "steelblue", face = 'bold'),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 14),
strip.text.x = element_text(size=16, face = 'bold'),
legend.text=element_text(size=13)) +
labs(title = "Perpetrators' Number of Civilian Killings (2016-2021)",
subtitle = "Jan. 2016 - Dec. 2021",
caption = "") +
guides(fill = guide_legend(title = "")) +
facet_wrap(~ admin1)
ggplotly(gg2, height = 600, width = 850)
fatalities_region_year_identity_militias = acled_2016_2021_west_south %>%
filter(inter1 == "Identity Militias") %>%
group_by(admin1, year, actor1) %>%
tally(fatalities)
library(rgdal)
library(tidyverse)
library(tmap)
library(sp)
library(sf)
setwd("C:/Users/rsb84/Desktop/RB/COLUMBIA/QMSS/COURSES/Spring_2021/Data Visualization/End_project")
darfur_internal<-raster::shapefile("darfur_internal.shp")
# Convert to a simple feature spatial object
darfur_internal_sf <- sf::st_as_sf(darfur_internal)
original_crs <- st_crs(darfur_internal) #Original CRS is EPSG:4326, i.e., the World Geodetic System 1984 (WGS84), a geographic CRS
CRS.new <- st_crs("EPSG:20135") #This is a projected CRS well-suited for Darfur
darfur_internal_sf <- st_transform(darfur_internal_sf, crs = CRS.new) # Transform the CRS to EPSG:20135
acled_short = ACLED_data[c("event_date","admin1","year","sub_event_type","actor1","inter1","location","latitude","longitude","attacks","fatalities")] %>%
mutate(Perpetrator = factor(inter1,
levels = c(1, 2, 3, 4, 5, 6, 7, 8),
labels = c("State Forces", "Rebel Groups", "Political Militias", "Identity Militias", "Rioters", "Protesters", "Civilians", "External/Other Forces"))) #%>%
#select(-inter1)
acled <- acled_short %>%
mutate(inter1 = factor(inter1, levels = c(1,3,4,2,8), labels = c("State Forces", "Political Militias", "Identity Militias", "Rebel Groups", "External/Other Forces"))) %>% #This assigns the specified armed groups in the labels argument to the integer values in the levels argument. It also controls the order of the armed groups on any legends
mutate(Perpetrator = inter1,
Year = year,
Actor = actor1)
acled_2016 = subset(acled, year == 2016)
acled_2017 = subset(acled, year == 2017)
acled_2018 = subset(acled, year == 2018)
acled_2019 = subset(acled, year == 2019)
acled_2020 = subset(acled, year == 2020)
acled_2021 = subset(acled, year == 2021)
# Make objects special feature objects and set CRS to the original CRS of the darfur map shapefile -
acled_2016_sf <- st_as_sf(acled_2016, coords = c("longitude", "latitude"), crs = original_crs)
acled_2017_sf <- st_as_sf(acled_2017, coords = c("longitude", "latitude"), crs = original_crs)
acled_2018_sf <- st_as_sf(acled_2018, coords = c("longitude", "latitude"), crs = original_crs)
acled_2019_sf <- st_as_sf(acled_2019, coords = c("longitude", "latitude"), crs = original_crs)
acled_2020_sf <- st_as_sf(acled_2020, coords = c("longitude", "latitude"), crs = original_crs)
acled_2021_sf <- st_as_sf(acled_2021, coords = c("longitude", "latitude"), crs = original_crs)
# Transform the CRS to EPSG:20135
acled_2016_sf <- st_transform(acled_2016_sf, crs=CRS.new)
acled_2017_sf <- st_transform(acled_2017_sf, crs=CRS.new)
acled_2018_sf <- st_transform(acled_2018_sf, crs=CRS.new)
acled_2019_sf <- st_transform(acled_2019_sf, crs=CRS.new)
acled_2020_sf <- st_transform(acled_2020_sf, crs=CRS.new)
acled_2021_sf <- st_transform(acled_2021_sf, crs=CRS.new)
# At this point, I make "contains" spatial joins with R. See my comments in the code of Visual 6 for more understanding of the following code:
acled_2016_joined_sf <- sf::st_join(acled_2016_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2017_joined_sf <- sf::st_join(acled_2017_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2018_joined_sf <- sf::st_join(acled_2018_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2019_joined_sf <- sf::st_join(acled_2019_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2020_joined_sf <- sf::st_join(acled_2020_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2021_joined_sf <- sf::st_join(acled_2021_sf, join = st_within, left=FALSE, darfur_internal_sf)
acled_2016_joined_sf$fatalities <- as.numeric(acled_2016_joined_sf$fatalities)
acled_2017_joined_sf$fatalities <- as.numeric(acled_2017_joined_sf$fatalities)
acled_2018_joined_sf$fatalities <- as.numeric(acled_2018_joined_sf$fatalities)
acled_2019_joined_sf$fatalities <- as.numeric(acled_2019_joined_sf$fatalities)
acled_2020_joined_sf$fatalities <- as.numeric(acled_2020_joined_sf$fatalities)
acled_2021_joined_sf$fatalities <- as.numeric(acled_2021_joined_sf$fatalities)
fifty_miles_in_meters <- 50 * 1609.344
# Closed South Darfur Bases between 2017 - 2019
# UN Bases in South Darfur Closed in 2017:
closed_bases_south_2017 <- readxl::read_excel("closed_bases_south_2017-portfolio_2023.xlsx",
col_types = c("text", "text",
"numeric", "numeric"))
closed_bases_south_2017_sf=st_as_sf(closed_bases_south_2017, coords = c("longitude", "latitude"), crs = original_crs)
# UN Bases in South Darfur Closed in 2018:
closed_bases_south_2018 <- readxl::read_excel("closed_bases_south_2018-portfolio_2023.xlsx",
col_types = c("text","text",
"numeric", "numeric"))
closed_bases_south_2018_sf=st_as_sf(closed_bases_south_2018, coords = c("longitude", "latitude"), crs = original_crs)
# UN Bases in South Darfur Closed in 2019:
closed_bases_south_2019 <- readxl::read_excel("closed_bases_south_2019-portfolio_2023.xlsx",
col_types = c("text", "text",
"numeric", "numeric"))
closed_bases_south_2019_sf=st_as_sf(closed_bases_south_2019, coords = c("longitude", "latitude"), crs = original_crs)
# Closed West Darfur Bases between 2017 - 2019
# UN Bases in West Darfur Closed in 2017:
closed_bases_west_2017 <- readxl::read_excel("closed_bases_west_2017-portfolio_2023.xlsx",
col_types = c("text", "text",
"numeric", "numeric"))
closed_bases_west_2017_sf=st_as_sf(closed_bases_west_2017, coords = c("longitude", "latitude"), crs = original_crs)
# UN Bases in West Darfur Closed in 2018:
closed_bases_west_2018 <- readxl::read_excel("closed_bases_west_2018-portfolio_2023.xlsx",
col_types = c("text", "text",
"numeric", "numeric"))
closed_bases_west_2018_sf=st_as_sf(closed_bases_west_2018, coords = c("longitude", "latitude"), crs = original_crs)
# UN Bases in West Darfur Closed in 2019:
closed_bases_west_2019 <- readxl::read_excel("closed_bases_west_2019-portfolio_2023.xlsx",
col_types = c("text", "text",
"numeric", "numeric"))
closed_bases_west_2019_sf=st_as_sf(closed_bases_west_2019, coords = c("longitude", "latitude"), crs = original_crs)
# Transform the following data to be plotted into a Projected CRS (i.e., EPSG:20135) that uses units of measurement of meters rather than degrees
closed_bases_south_2017_sf <- st_transform(closed_bases_south_2017_sf, CRS.new)
closed_bases_south_2018_sf <- st_transform(closed_bases_south_2018_sf, CRS.new)
closed_bases_south_2019_sf <- st_transform(closed_bases_south_2019_sf, CRS.new)
closed_bases_west_2017_sf <- st_transform(closed_bases_west_2017_sf, CRS.new)
closed_bases_west_2018_sf <- st_transform(closed_bases_west_2018_sf, CRS.new)
closed_bases_west_2019_sf <- st_transform(closed_bases_west_2019_sf, CRS.new)
# Create buffer objects around UN bases closed in South Darfur in 2017, 2018, and 2019:
# South Darfur
# 2017
closed_bases_south_2017_buffers <- st_buffer(x=closed_bases_south_2017_sf, dist = fifty_miles_in_meters)
union_buffers_south.2017 <- st_union(closed_bases_south_2017_buffers)
# 2018
closed_bases_south_2018_buffers <- st_buffer(x=closed_bases_south_2018_sf, dist = fifty_miles_in_meters)
union_buffers_south.2018 <- st_union(closed_bases_south_2018_buffers)
# 2019
closed_bases_south_2019_buffers <- st_buffer(x=closed_bases_south_2019_sf, dist = fifty_miles_in_meters)
union_buffers_south.2019 <- st_union(closed_bases_south_2019_buffers)
# West Darfur
# 2017
closed_bases_west_2017_buffers <- st_buffer(x=closed_bases_west_2017_sf, dist = fifty_miles_in_meters)
union_buffers_west.2017 <- st_union(closed_bases_west_2017_buffers)
# 2018
closed_bases_west_2018_buffers <- st_buffer(x=closed_bases_west_2018_sf, dist = fifty_miles_in_meters)
union_buffers_west.2018 <- st_union(closed_bases_west_2018_buffers)
# 2019
closed_bases_west_2019_buffers <- st_buffer(x=closed_bases_west_2019_sf, dist = fifty_miles_in_meters)
union_buffers_west.2019 <- st_union(closed_bases_west_2019_buffers)
# CALCULATNG ATTACKS ON CIVILIANS & FATALITIES PER YEAR INSIDE 50 MILE RADIUS buffers AROUND SOUTH DARFUR BASES CLOSED IN 2017, 2018, and 2019
# length() calculates the number of civilian attacks
# sum() calculates the number of fatalities from these attacks
acled_data_2016.south_buffers_2017 = sf::st_filter(acled_2016_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_2017.south_buffers_2017 = sf::st_filter(acled_2017_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_2018.south_buffers_2017 = sf::st_filter(acled_2018_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_2019.south_buffers_2017 = sf::st_filter(acled_2019_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_2020.south_buffers_2017 = sf::st_filter(acled_2020_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_2021.south_buffers_2017 = sf::st_filter(acled_2021_joined_sf, union_buffers_south.2017, .predicate = st_within)
acled_data_all_years.south_buffers_2017 = rbind(acled_data_2016.south_buffers_2017, acled_data_2017.south_buffers_2017, acled_data_2018.south_buffers_2017, acled_data_2019.south_buffers_2017, acled_data_2020.south_buffers_2017, acled_data_2021.south_buffers_2017)
# Filtering, transforming, summarizing, and then retaining top 2 rows for each year based on fatalities
violence_per_identity_militia_actor.south_buffers_2017 <- acled_data_all_years.south_buffers_2017 %>%
filter(Perpetrator == "Identity Militias", admin1 == "South Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "South Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2017 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities.
acled_data_2016.south_buffers_2018 = sf::st_filter(acled_2016_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_2017.south_buffers_2018 = sf::st_filter(acled_2017_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_2018.south_buffers_2018 = sf::st_filter(acled_2018_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_2019.south_buffers_2018 = sf::st_filter(acled_2019_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_2020.south_buffers_2018 = sf::st_filter(acled_2020_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_2021.south_buffers_2018 = sf::st_filter(acled_2021_joined_sf, union_buffers_south.2018, .predicate = st_within)
acled_data_all_years.south_buffers_2018 = rbind(acled_data_2016.south_buffers_2018, acled_data_2017.south_buffers_2018, acled_data_2018.south_buffers_2018, acled_data_2019.south_buffers_2018, acled_data_2020.south_buffers_2018, acled_data_2021.south_buffers_2018)
# Filtering, transforming, summarizing, and then retaining top 2 rows for each year based on fatalities
violence_per_identity_militia_actor.south_buffers_2018 <- acled_data_all_years.south_buffers_2018 %>%
filter(Perpetrator == "Identity Militias", admin1 == "South Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "South Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2018 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities.
acled_data_2016.south_buffers_2019 = sf::st_filter(acled_2016_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_2017.south_buffers_2019 = sf::st_filter(acled_2017_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_2018.south_buffers_2019 = sf::st_filter(acled_2018_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_2019.south_buffers_2019 = sf::st_filter(acled_2019_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_2020.south_buffers_2019 = sf::st_filter(acled_2020_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_2021.south_buffers_2019 = sf::st_filter(acled_2021_joined_sf, union_buffers_south.2019, .predicate = st_within)
acled_data_all_years.south_buffers_2019 = rbind(acled_data_2016.south_buffers_2019, acled_data_2017.south_buffers_2019, acled_data_2018.south_buffers_2019, acled_data_2019.south_buffers_2019, acled_data_2020.south_buffers_2019, acled_data_2021.south_buffers_2019)
# Filtering, transforming, summarizing, and then retaining top 2 rows for each year based on fatalities
violence_per_identity_militia_actor.south_buffers_2019 <- acled_data_all_years.south_buffers_2019 %>%
filter(Perpetrator == "Identity Militias", admin1 == "South Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "South Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2019 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities
# CALCULATNG ATTACKS ON CIVILIANS & FATALITIES PER YEAR INSIDE 50 MILE RADIUS BUFFERS AROUND WEST DARFUR BASES CLOSED IN 2017, 2018, and 2019
# length() calculates the number of civilian attacks
# sum() calculates the number of fatalities from these attacks
acled_data_2016.west_buffers_2017 = sf::st_filter(acled_2016_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_2017.west_buffers_2017 = sf::st_filter(acled_2017_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_2018.west_buffers_2017 = sf::st_filter(acled_2018_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_2019.west_buffers_2017 = sf::st_filter(acled_2019_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_2020.west_buffers_2017 = sf::st_filter(acled_2020_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_2021.west_buffers_2017 = sf::st_filter(acled_2021_joined_sf, union_buffers_west.2017, .predicate = st_within)
acled_data_all_years.west_buffers_2017 = rbind(acled_data_2016.west_buffers_2017, acled_data_2017.west_buffers_2017, acled_data_2018.west_buffers_2017, acled_data_2019.west_buffers_2017, acled_data_2020.west_buffers_2017, acled_data_2021.west_buffers_2017)
# Filtering, transforming, summarizing, and then retaining top 2 rows for each year based on fatalities
violence_per_identity_militia_actor.west_buffers_2017 <- acled_data_all_years.west_buffers_2017 %>%
filter(Perpetrator == "Identity Militias", admin1 == "West Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "West Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2017 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>% # Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities.
acled_data_2016.west_buffers_2018 = sf::st_filter(acled_2016_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_2017.west_buffers_2018 = sf::st_filter(acled_2017_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_2018.west_buffers_2018 = sf::st_filter(acled_2018_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_2019.west_buffers_2018 = sf::st_filter(acled_2019_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_2020.west_buffers_2018 = sf::st_filter(acled_2020_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_2021.west_buffers_2018 = sf::st_filter(acled_2021_joined_sf, union_buffers_west.2018, .predicate = st_within)
acled_data_all_years.west_buffers_2018 = rbind(acled_data_2016.west_buffers_2018, acled_data_2017.west_buffers_2018, acled_data_2018.west_buffers_2018, acled_data_2019.west_buffers_2018, acled_data_2020.west_buffers_2018, acled_data_2021.west_buffers_2018)
# Filtering, transforming, summarizing, and then retaining top 2 rows for each year based on fatalities
violence_per_identity_militia_actor.west_buffers_2018 <- acled_data_all_years.west_buffers_2018 %>%
filter(Perpetrator == "Identity Militias", admin1 == "West Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "West Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2018 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities.
acled_data_2016.west_buffers_2019 = sf::st_filter(acled_2016_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_2017.west_buffers_2019 = sf::st_filter(acled_2017_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_2018.west_buffers_2019 = sf::st_filter(acled_2018_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_2019.west_buffers_2019 = sf::st_filter(acled_2019_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_2020.west_buffers_2019 = sf::st_filter(acled_2020_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_2021.west_buffers_2019 = sf::st_filter(acled_2021_joined_sf, union_buffers_west.2019, .predicate = st_within)
acled_data_all_years.west_buffers_2019 = rbind(acled_data_2016.west_buffers_2019, acled_data_2017.west_buffers_2019, acled_data_2018.west_buffers_2019, acled_data_2019.west_buffers_2019, acled_data_2020.west_buffers_2019, acled_data_2021.west_buffers_2019)
violence_per_identity_militia_actor.west_buffers_2019 <- acled_data_all_years.west_buffers_2019 %>%
filter(Perpetrator == "Identity Militias", admin1 == "West Darfur") %>% # Keep only rows where Perpetrator is "Identity Militias" and where admin1 is "West Darfur"
mutate(Year = year,
Actor = actor1) %>%
group_by(Year, Actor) %>% # Group by Year and Actor
summarise(
Fatalities = sum(fatalities, na.rm = TRUE), # Sum fatalities, removing NA values
Attacks = sum(attacks, na.rm = TRUE), # Sum attacks, removing NA values
.groups = 'drop' # Drop the grouping structure afterwards
) %>%
mutate(Buffers = "2019 Closed Base Areas") %>% # Add new column denoting the buffer
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) # Slice the top 2 rows for each Year
# The resulting sf dataframe will keep the top row for each Year in terms of number of fatalities.
# Combine all of the resulting sf objects from above, for South Darfur buffers
violence_per_identity_militia_actor.south_buffers <- rbind(violence_per_identity_militia_actor.south_buffers_2017, violence_per_identity_militia_actor.south_buffers_2018, violence_per_identity_militia_actor.south_buffers_2019) %>%
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) %>%
rename(Areas = Buffers) %>% # Rename 'Buffer' as 'Area'
as.data.frame() %>% # Remove 'geometry' column by converting the sf object to a dataframe
select(-geometry)
# Combine all of the resulting sf objects from above, for West Darfur buffers
violence_per_identity_militia_actor.west_buffers <- rbind(violence_per_identity_militia_actor.west_buffers_2017, violence_per_identity_militia_actor.west_buffers_2018, violence_per_identity_militia_actor.west_buffers_2019) %>%
group_by(Year) %>% # Regroup by Year only
arrange(desc(Fatalities), .by_group = TRUE) %>%# Sort within each Year group by Fatalities
slice_max(order_by = Fatalities, n = 1, with_ties = FALSE) %>%
rename(Areas = Buffers) %>% # Rename 'Buffer' as 'Area'
as.data.frame() %>%
select(-geometry) # Remove 'geometry' column by converting the sf object to a dataframe
combined <- rbind(violence_per_identity_militia_actor.south_buffers, violence_per_identity_militia_actor.west_buffers)