::p_load(shiny, tidyverse, shinydashboard,dplyr,
pacman
spatstat, spdep,
lubridate, leaflet,
plotly, DT, viridis,
ggplot2, sf, tmap, readr,
scales, ggthemes, gridExtra,
knitr, data.table,
CGPfunctions, ggHoriPlot,
patchwork, ggiraph, vcd, vcdExtra,
ggstatsplot, ggmosaic, FunnelPlotR, knitr)
Take Home Ex 4.1 - Project Charts
Installing and Launching R Packages
Data Wrangling
Importing Dataset
<- read_csv("data/2010-01-01-2023-12-31-Southeast_Asia-Myanmar.csv") Myanmar
Adjusting Attributes
<- Myanmar %>%
Myanmar mutate(year =factor(year))
$event_date <- dmy(Myanmar$event_date) Myanmar
Reducing Columns
<- Myanmar %>%
Myanmar_final select(-time_precision, -geo_precision, -source_scale, -timestamp, -tags)
Geo-data Correction
<- Myanmar_final %>%
ACLED_MMR_1 mutate(admin1 = case_when(
== "Bago-East" ~ "Bago (East)",
admin1 == "Bago-West" ~ "Bago (West)",
admin1 == "Shan-North" ~ "Shan (North)",
admin1 == "Shan-South" ~ "Shan (South)",
admin1 == "Shan-East" ~ "Shan (East)",
admin1 TRUE ~ as.character(admin1)
))
<- Myanmar_final %>%
ACLED_MMR_1 mutate(admin2 = case_when(
== "Yangon-East" ~ "Yangon (East)",
admin2 == "Yangon-West" ~ "Yangon (West)",
admin2 == "Yangon-North" ~ "Yangon (North)",
admin2 == "Yangon-South" ~ "Yangon (South)",
admin2 == "Mong Pawk (Wa SAD)" ~ "Tachileik",
admin2 == "Nay Pyi Taw" ~ "Det Khi Na",
admin2 == "Yangon" ~ "Yangon (West)",
admin2 TRUE ~ as.character(admin2)
))
EDA
Summary of Incidents
<- ACLED_MMR_1 %>%
Summary_Data group_by(year,event_type) %>%
summarize(
Total_incidents = n(),
Total_Fatalities = sum(fatalities, na.rm=TRUE)
)
Distribution of Incidents Across Years by event_type
<- ggplot(Summary_Data,
gg1 aes(x = year,
y = Total_incidents,
fill = event_type)) +
geom_col_interactive(aes(tooltip = year,
data_id = year),
alpha = 0.5) +
geom_text(aes(label = Total_incidents),
vjust = 0.5,
color = "black",
size = 2,
check_overlap = TRUE,
position = "dodge") +
facet_wrap(~event_type,
ncol =2) +
theme_minimal() +
labs(y = "Total Number of Incidents", x = "Years") +
theme(
panel.grid.major.y = element_line(color = "pink", linetype = 2),
strip.background = element_rect(fill = "black"),
axis.text.x = element_blank(),
strip.text = element_text(colour = "white"),
legend.position = "none")
girafe(
ggobj = gg1,
width_svg = 6,
height_svg = 6*0.618)
Distribution of Fatalities Across Years by event_type
<- ggplot(Summary_Data,
gg2 aes(x = year,
y = Total_Fatalities,
fill = event_type)) +
geom_col_interactive(aes(tooltip = year,
data_id = year),
alpha = 0.5) +
geom_text(aes(label = Total_incidents),
vjust = 0.5,
color = "black",
size = 2,
check_overlap = TRUE,
position = "dodge") +
facet_wrap(~event_type,
ncol =2) +
theme_minimal() +
labs(y = "Total Number of Fatalities", x = "Years") +
theme(
panel.grid.major.y = element_line(color = "pink", linetype = 2),
strip.background = element_rect(fill = "black"),
strip.text = element_text(colour = "white"),
axis.text.x = element_blank(),
legend.position = "none")
girafe(
ggobj = gg2,
width_svg = 6,
height_svg = 6*0.618)
Distribution of Incidents by Region
<- ACLED_MMR_1 %>%
Region_Summary group_by(country, admin1, admin2, admin3, event_type,sub_event_type, disorder_type) %>%
summarize(
Total_incidents = n(),
Total_Fatalities = sum(fatalities, na.rm=TRUE)
)
<- ggplot(Region_Summary,
gg3 aes(x = admin1,
y = Total_incidents,
fill = event_type)) +
geom_col_interactive(aes(tooltip = admin1,
data_id = admin1),
alpha = 0.5) +
geom_text(aes(label = Total_incidents),
vjust = 0.5,
color = "black",
size = 2,
check_overlap = TRUE,
position = "dodge") +
facet_wrap(~event_type,
ncol =2) +
theme_minimal() +
labs(y = "Total Number of Incidents", x = "Regions") +
theme(
panel.grid.major.y = element_line(color = "pink", linetype = 2),
axis.text.x = element_blank(),
strip.background = element_rect(fill = "black"),
strip.text = element_text(colour = "white"),
legend.position = "none")
girafe(
ggobj = gg3,
width_svg = 6,
height_svg = 6*0.618)
Distribution of Fatalities by Region
<- ggplot(Region_Summary,
gg4 aes(x = admin1,
y = Total_Fatalities,
fill = event_type)) +
geom_col_interactive(aes(tooltip = admin1,
data_id = admin1),
alpha = 0.5) +
geom_text(aes(label = Total_Fatalities),
vjust = 0.5,
color = "black",
size = 2,
check_overlap = TRUE,
position = "dodge") +
facet_wrap(~event_type,
ncol =2) +
theme_minimal() +
labs(y = "Total Number of Fatalities", x = "Regions") +
theme(
panel.grid.major.y = element_line(color = "pink", linetype = 2),
axis.text.x = element_blank(),
strip.background = element_rect(fill = "black"),
strip.text = element_text(colour = "white"),
legend.position = "none")
girafe(
ggobj = gg4,
width_svg = 6,
height_svg = 6*0.618)
CDA
Summary of Event Types by Fatalities (Total Count)
ggbetweenstats(ACLED_MMR_1,
x= event_type,
y= fatalities)
Summary of Event Types by Fatalities (Total Count), Grouped by Region
grouped_ggbetweenstats(ACLED_MMR_1,
x = event_type,
y = fatalities,
grouping.var = admin1,
type = "np",
pairwise.display = "s",
pairwise_comparisons = TRUE,
output = "plot")
Summary of Event Types Across Years, Incidents and Fatalities
Incidents
ggbetweenstats(Summary_Data,
x= event_type,
y= Total_incidents)
Fatalities
ggbetweenstats(Summary_Data,
x= event_type,
y= Total_Fatalities)
Mosaic Plot (Incident Counts)
By Year and Event Type
::mosaic(~year + event_type, data = ACLED_MMR_1, gp = shading_max,
vcdlabeling = labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"right")))
By Region and Event Type
::mosaic(~ admin1 + event_type, data = ACLED_MMR_1, gp = shading_max,
vcdlabeling = labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"right")
))
admin1 + disorder_type
::mosaic(~ admin1 + disorder_type, zero_size = FALSE, data = ACLED_MMR_1,gp = shading_max,
vcdlabeling = labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"right")))
subeventtype
::mosaic(~ sub_event_type +admin1, zero_size = FALSE, data = ACLED_MMR_1,gp = shading_max,
vcdlabeling = labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"right")))
Using geom_mosiac
Fatalities by Event Type and Region
<- ggplot(Region_Summary) +
gg5 geom_mosaic(aes(weight = Total_Fatalities,
x = (product(event_type, country)), fill = admin1)) +
labs(x = "Myanmar",
fill = "Regions") +
## guides(fill = "none") + to remove legend if not required
theme(
axis.text.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x = element_blank()
)
ggplotly(gg5)
<- ggplot(Region_Summary) +
gg6 geom_mosaic(aes(weight = Total_Fatalities,
x = (product(sub_event_type, country)), fill = admin2)) +
labs(x = "Myanmar",
fill = "Territory") +
## guides(fill = "none") + to remove legend if not required
theme(
axis.text.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x = element_blank()
)
ggplotly(gg6)
FunnelPlotR
<- ACLED_MMR_1 %>%
Region_Summary_Test filter(year == 2023)
<- Region_Summary_Test %>%
Region_Summary_Test group_by(country, admin1, admin2, admin3, event_type) %>%
summarize(
Total_incidents = n(),
Total_fatalities = sum(fatalities)
)
<- Region_Summary_Test %>%
Region_Summary_Test mutate(rate = Total_fatalities / Total_incidents) %>%
mutate(rate.se = sqrt((rate*(1-rate))/ (Total_incidents))) %>%
filter(rate>0)
<- weighted.mean(Region_Summary_Test$rate, 1/Region_Summary_Test$rate.se^2) fit.mean
<- seq(1, max(Region_Summary_Test$Total_incidents), 1)
number.seq <- fit.mean - 1.96 * sqrt((fit.mean*(1-fit.mean)) / (number.seq))
number.ll95 <- fit.mean + 1.96 * sqrt((fit.mean*(1-fit.mean)) / (number.seq))
number.ul95 <- fit.mean - 3.29 * sqrt((fit.mean*(1-fit.mean)) / (number.seq))
number.ll999 <- fit.mean + 3.29 * sqrt((fit.mean*(1-fit.mean)) / (number.seq))
number.ul999 <- data.frame(number.ll95, number.ul95, number.ll999,
dfCI number.ul999, number.seq, fit.mean)
<- ggplot(Region_Summary_Test, aes(x = Total_incidents, y = rate)) +
p geom_point(alpha=0.4) +
geom_line(data = dfCI,
aes(x = number.seq,
y = number.ll95),
size = 0.4,
colour = "grey40",
linetype = "dashed") +
geom_line(data = dfCI,
aes(x = number.seq,
y = number.ul95),
size = 0.4,
colour = "grey40",
linetype = "dashed") +
geom_line(data = dfCI,
aes(x = number.seq,
y = number.ll999),
size = 0.4,
colour = "grey40") +
geom_line(data = dfCI,
aes(x = number.seq,
y = number.ul999),
size = 0.4,
colour = "grey40") +
geom_hline(data = dfCI,
aes(yintercept = fit.mean),
size = 0.4,
colour = "grey40")
p
funnel_plot(
numerator = Region_Summary_Test$Total_incidents,
denominator = Region_Summary_Test$Total_fatalities,
group = Region_Summary_Test$admin2,
data_type = "PR", #<<
xrange = c(0,1500), #<<
yrange = c(0,20) #<<
)
A funnel plot object with 76 points of which 16 are outliers.
Plot is adjusted for overdispersion.