-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShiny_1_Haiti_Event_Explorer_Map_Fatalities
115 lines (103 loc) · 4.86 KB
/
Shiny_1_Haiti_Event_Explorer_Map_Fatalities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#Shiny Project 1: Haiti 2018-2022: Conflict Event Explorer
#With pull down menu of event types, reactive leaflet map, and line graph of fatalities for corresponding sub_event_types
#TO DO: Fix scaling axes on fatality chart so that the years remain the same regardless of the event type choice.
#TO DO: Make better labels for each section. Add a text box to the sidebar with an explaination about what the app does.
#TO DO: Adjust theme to make it prettier.
#TO DO: Change markers on the map to points.
#TO DO: Fix event category Explosions/Remote Violence so that total fatalities displays correctly and the map stays on Haiti even on years with no incidents of that category.
#Required libraries: shiny, shinythemes, tidyverse, leaflet
library(shiny)
library(leaflet)
library(shinythemes)
library(tidyverse)
#Define the theme, physical layout of the page, and the user input
#Removed event category Explosions/Remote violence because n=2, and fatalities were not displaying correctly
ui <- fluidPage(
titlePanel('Haiti 2018-2022: Conflict Event Explorer'),
#shinythemes::shinytheme("superhero"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "event",
label = "Select an event type",
choices = c("Battles", "Protests", "Riots", "Violence against civilians"),
selected = "Battles"
),
selectInput(
inputId = "year",
label = "Select year",
choices = c("2018", "2019", "2020", "2021", "2022"),
selected = "2018"
),
h4 ("Select from the pull down menu of event categories and years, to see where violent events have occurred, and how different sub-event-types compare in terms of fatalities."),
div(
h5 (("Data Source: "),tags$a(href="http://www.acleddata.com", "Armed Conflict Location & Event Data Project"), ("(accessed 2023-04-06)."))
)
),
mainPanel(
leafletOutput(outputId = "map"),
br(),
br(),
h4("Total Annual Fatalities by Sub-Event Type"),
plotOutput(outputId = "lineplot")
)
)
)
#Define the data source, in this case ACLED.
server <- function(input, output) {
data_url <- "https://www.dropbox.com/s/m62qxc07hae0r6m/ACLED_2018-01-01-2023-03-31-Caribbean-Haiti_CLEAN.csv?dl=1"
data <- read.csv(data_url)
#Filter the data so that only complete years are included in the analysis (2018-2022), and to make sure that latitude and longitude are not NULL and are numerical data.
#Formatting the latitude and longitude here is just to make sure that read.csv interprets it the way I formatted it during the data preparation phase.
filtered_data <- reactive({
data %>%
filter(event_type == input$event) %>%
filter(event_date_year != 2023) %>%
filter (event_date_year == input$year) %>%
filter(latitude != "") %>%
filter(longitude != "") %>%
mutate(
longitude = as.numeric(longitude),
latitude = as.numeric(latitude)
) %>%
group_by(event_date_year, sub_event_type)
})
#Format the data for the line graph of fatalities, referring to the above filtered_data which includes a reactive expression for event type.
filtered_data_fatalities <- reactive({
data %>%
filter(event_type == input$event) %>%
filter(event_date_year != 2023) %>%
filter(latitude != "") %>%
filter(longitude != "") %>%
mutate(
longitude = as.numeric(longitude),
latitude = as.numeric(latitude)
) %>%
group_by(event_date_year, sub_event_type)
})
#Plot the line graph of fatalities, referring to the above filtered_data which includes a reactive expression for event type.
output$lineplot <- renderPlot({
filtered_data_fatalities <- filtered_data_fatalities() #refers to the data which has responded to the user input
fatalities_sum <- filtered_data_fatalities() %>%
group_by(event_date_year, sub_event_type) %>%
summarise(total_fatalities = sum(fatalities)) #calculates fatality sums and assigns them a name
#Format the graph referring to the above data.
ggplot(data = fatalities_sum) +
geom_line(mapping = aes(x = event_date_year, y = total_fatalities , color = sub_event_type)) +
labs(x = "Year", y = "Number of Fatalities") +
xlim(2018, 2022)
})
#Format the map using the above data, directing it specifically to the data which has been filtered according to user input.
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$OpenStreetMap.HOT) %>% #choose tile source for basemap
addMarkers(
data = filtered_data(),
lng = filtered_data()$longitude,
lat = filtered_data()$latitude,
popup = ~paste(strong("Location: "), filtered_data()$location, strong(" Sub-Event-Type: "), filtered_data()$sub_event_type, strong(" Date:"), filtered_data()$event_date)
)
})
}
#This activates the app
shinyApp(ui = ui, server = server)