-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
321 lines (279 loc) · 8.64 KB
/
server.R
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
library(tidyverse)
library(plotly)
library(scales)
library(lubridate)
library(reshape2)
library(naniar)
# Load and clean up data ----
# Load original data
collisions_raw_df <- read.csv("seattle-collisions.csv", stringsAsFactors = F)
# Remove unnecessary columns
collisions_df <- collisions_raw_df %>%
select(
-INCKEY:-INTKEY, -EXCEPTRSNCODE, -EXCEPTRSNDESC, -INATTENTIONIND,
-PEDROWNOTGRNT:-HITPARKEDCAR
)
# Add NA to blank cells
collisions_df <- collisions_df %>%
mutate_all(na_if, "")
# Save dates
dates <- as.Date(collisions_df$INCDATE)
# Remove the year 2003 and 2022
collisions_df <- collisions_df %>%
mutate(YEAR = year(INCDATE)) %>%
filter(YEAR != 2003, YEAR != 2022)
# Update dates
dates <- as.Date(collisions_df$INCDATE)
# Customize theme ----
# Create an overall theme for plots
overall_theme <- theme(
plot.title = element_text(
face = "bold", color = "deepskyblue4", size = (20), hjust = 0.5
),
legend.title = element_text(color = "steelblue", face = "bold.italic"),
legend.text = element_text(face = "italic", color = "steelblue4"),
axis.title = element_text(size = (13), color = "steelblue4"),
axis.text = element_text(color = "cornflowerblue", size = (10)),
)
# Calculate the total number of DUI cases
under_influence <- collisions_df %>%
replace_with_na(replace = list(UNDERINFL = "N")) %>%
mutate(new_data = replace(UNDERINFL, UNDERINFL == "Y", 1)) %>%
group_by(YEAR) %>%
summarize(under_influence = sum(as.numeric(new_data), na.rm = T))
# Summarize total injuries, people involved, and collisions per year
summary <- collisions_df %>%
group_by(YEAR) %>%
summarize(
injuries = sum(INJURIES),
total_people = sum(PERSONCOUNT),
total_collisions = n()
)
# Join under influence and summary data frames
joined <- left_join(under_influence, summary) %>%
rename(
"Year" = YEAR,
"Under Influence" = under_influence,
"Injuries" = injuries,
"Total People" = total_people,
"Total Collisions" = total_collisions
)
# Chart 2 data ----
# Total number of injuries each year
num_injuries <- collisions_df %>%
group_by(YEAR) %>%
summarize(num_injuries = sum(INJURIES))
# Total number of serious injuries each year
num_serious_injuries <- collisions_df %>%
group_by(YEAR) %>%
summarize(num_serious_injuries = sum(SERIOUSINJURIES))
# Total number of fatalities each year
num_fatalities <- collisions_df %>%
group_by(YEAR) %>%
summarize(num_fatalities = sum(FATALITIES))
# Pull all types of casualties into a list
all_casualties_list <- list(num_injuries, num_serious_injuries, num_fatalities)
# Merge all data frames
all_casualties <- all_casualties_list %>% reduce(full_join, by = "YEAR")
# Plot multiple y values as separate lines using long format data frame
# Load "reshape2" and use melt function
all_casualties_long <- melt(
all_casualties,
id = "YEAR",
measure = c("num_injuries", "num_serious_injuries", "num_fatalities")
)
all_casualties_long <- all_casualties_long %>%
rename(Year = YEAR, Quantity = value, Type = variable)
# Change variable names without changing the default colors
# Use scale_color_manual if wanted to change both names and colors
levels(all_casualties_long$Type) <- list(
"Injuries" = "num_injuries",
"Serious Injuries" = "num_serious_injuries",
"Fatalities" = "num_fatalities"
)
# Plot the casualties
plot_casualties <- function(data) {
ggplot(data, aes(Year, Quantity, color = Type)) +
geom_point() +
geom_line() +
labs(
title = "Total Number of Casualties Per Year", x = "Year",
y = "Number of Casualties", color = "Levels of Severity"
) +
overall_theme +
scale_x_continuous(breaks = pretty_breaks())
}
# Chart 3 data ----
# Total number of each weather
num_weather_condition <- collisions_df %>%
group_by(WEATHER) %>%
tally() %>%
na.omit() %>%
arrange(-n) %>%
filter(!row_number() %in% c(4, 6)) %>%
head(5)
# Total number of each road condition
num_road_condition <- collisions_df %>%
group_by(ROADCOND) %>%
tally() %>%
na.omit() %>%
arrange(-n) %>%
filter(!row_number() %in% c(3, 6)) %>%
head(5)
# Total number of each light condition
num_light_condition <- collisions_df %>%
group_by(LIGHTCOND) %>%
tally() %>%
na.omit() %>%
arrange(-n) %>%
filter(!row_number() %in% c(3, 8)) %>%
head(5)
# Create condition plot function
condition_plot <- function(data, type) {
# Change column names to modify the code easier
colnames(data)[1:2] <- c("condition_type", "number")
# Plot
plot <- ggplot(data) +
geom_col(aes(reorder(condition_type, desc(number)),
number,
fill = condition_type,
text = paste0(
condition_type,
"\n# of Collisions: ",
formatC(number, big.mark = ",")
)
)) +
labs(
title = paste0(
"Total Number of Collisions by ",
type, " Conditions"
),
x = paste0(type, " Condition"),
y = "Number of Collisions",
fill = paste0(type, " Condition")
) +
overall_theme +
theme(
axis.text.x = element_text(angle = 45),
legend.position = "none"
) +
scale_y_continuous(
labels = label_number(scale_cut = cut_short_scale())
)
# Make it interactive
ggplotly(plot, tooltip = "text")
}
# Server ----
trend_plot <- function(data, trend) {
ggplot(data, aes(x = Month, y = trend, color = Year)) +
geom_point() +
geom_line() +
labs(
title = paste0("Collision Trends Over Time"),
x = "Months", y = paste0("Number of ", trend), color = "Years"
) +
overall_theme +
scale_x_continuous(breaks = 1:12)
}
server <- function(input, output) {
# Chart 1
output$chart1 <- renderPlotly({
chart1_df <- collisions_df %>%
filter(YEAR %in% input$year1_selection) %>%
mutate(Month = month(INCDATE)) %>%
mutate(YEAR = as.character(YEAR)) %>%
group_by(Month, YEAR) %>%
summarize(
total_collisions = n(),
total_people = sum(PERSONCOUNT),
total_vehicles = sum(VEHCOUNT)
) %>%
rename(
"Year" = YEAR,
"Collisions" = total_collisions,
"People" = total_people,
"Vehicles" = total_vehicles
)
if (input$trend_selection == 1) {
plot <- ggplot(chart1_df, aes(x = Month, y = Collisions, color = Year)) +
geom_point() +
geom_line() +
labs(
title = "Total Collisions Over Time",
x = "Months", y = "Number of Collisions", color = "Years"
) +
overall_theme +
scale_x_continuous(breaks = 1:12)
} else if (input$trend_selection == 2) {
plot <- ggplot(chart1_df, aes(x = Month, y = People, color = Year)) +
geom_point() +
geom_line() +
labs(
title = "Total People Involved Over Time",
x = "Months", y = "Number of People Involved", color = "Years"
) +
overall_theme +
scale_x_continuous(breaks = 1:12)
} else {
plot <- ggplot(chart1_df, aes(x = Month, y = Vehicles, color = Year)) +
geom_point() +
geom_line() +
labs(
title = "Total Vehicle Involved Over Time",
x = "Months", y = "Number of Vehicles Involved", color = "Years"
) +
overall_theme +
scale_x_continuous(breaks = 1:12)
}
return(plot)
})
# Chart 2
data <- reactive(
all_casualties_long %>%
filter(all_casualties_long$Type %in%
input$casualty_selection)
)
output$chart2 <- renderPlotly({
plot <- plot_casualties(data())
return(plot)
})
# Chart 3
output$chart3 <- renderPlotly({
if (input$condition_selection == 1) {
plot <- condition_plot(num_weather_condition, "Weather")
} else if (input$condition_selection == 2) {
plot <- condition_plot(num_road_condition, "Road")
} else {
plot <- condition_plot(num_light_condition, "Light")
}
return(plot)
})
# Chart 4
output$chart4 <- renderPlotly({
collision_types <- collisions_df %>%
filter(YEAR %in% input$year4_selection) %>%
filter(COLLISIONTYPE %in% input$coltype_selection) %>%
group_by(COLLISIONTYPE) %>%
tally() %>%
na.omit()
plot <- ggplot(
collision_types,
aes(reorder(COLLISIONTYPE, desc(n)), n,
fill = COLLISIONTYPE,
text = paste0(COLLISIONTYPE, "\n# of Collisions: ", n)
)
) +
geom_col() +
labs(
title = paste0("Types of Collision in ", input$year4_selection),
x = "Collision Types",
y = "Number of Collisions",
) +
overall_theme +
theme(axis.text.x = element_text(angle = 45)) +
theme(legend.position = "none")
return(ggplotly(plot, tooltip = "text"))
})
# Table
output$table <- renderTable(joined, align = "c", bordered = T, digits = 0)
}