-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
556 lines (496 loc) · 20.3 KB
/
app.R
File metadata and controls
556 lines (496 loc) · 20.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
## SEATTLE PD DATA REPORT ##
## Wasif Siddique
## Jeremy Chang
## Rey Matsunaga
## Israel Sixto-Sanchez
library(dplyr)
library(ggplot2)
library(tidyverse)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(shinythemes)
# read data
data <- read.csv("./data/Seattle_police_data_2017.csv",na.strings = "NA",
stringsAsFactors = FALSE, fill = TRUE, header = TRUE)
offense_type <- tolower(unique(data$Summarized.Offense.Description))
month <- c("January", "February", "March", "April", "May", "June", "July",
"August","September", "October", "November", "December")
districts <- c("U-District", "West Seattle", "Greater Ballard", "North Seattle")
## we're only doing records for 2017
ui <- fluidPage(
#shinythemes::themeSelector(),
theme = shinythemes::shinytheme("cosmo"),
tags$head(tags$style(HTML("
#maptitle {
text-align: center;
}
div.box-header {
text-align: center;
}
#bar_title {
text-align: center;
}
#frequency_title {
text-align : center;
}
#density_title {
text-align: center;
}
h1 {
font-size : 40px;
font-weight: 500;
line-height: 1.1;
}
h2 {
font-size: 30px;
font-weight: 400;
}
h3 {
font-size: 25px;
font-weight: 300;
}
h4{
font-size: 18px;
font-weight: 200;
}
p {
font-weight: 100;
font-size: 15px;
}
"))),
h1("Seattle Crime Data Report of 2017"),
# The 5 tabs for 4 questions and documentation
tabsetPanel(
tabPanel("Heat Map for Crimes in Seattle", fluid = TRUE,
sidebarLayout(
sidebarPanel(
# State Widget
selectInput("crime_type", "Offense Type", choices = offense_type, selected = ""),
selectInput("months", "Month", choices = month, selected = "")
),
mainPanel(
br(),
textOutput("maptitle"),
br(),
leafletOutput("heatmap"),
br(),
textOutput("map_analysis")
)
)
),
tabPanel("Top 5 Crimes", fluid = TRUE,
sidebarLayout( # layout the page in two columns
sidebarPanel( # specify content for the "sidebar" column
# District Selector
selectInput("user_month", "Month", choices = month, selected = "")
),
mainPanel( # specify content for the "main" column
br(),
textOutput("bar_title"),
br(),
plotOutput("bargraph"),
br(),
textOutput("bar_analysis"),
br(),
textOutput("bar_trend")
)
)
),
tabPanel("Crime Frequency" , fluid = TRUE,
sidebarLayout( # layout the page in two columns
sidebarPanel( # specify content for the "sidebar" column
selectInput("crime_freq_type", "Offense Type", choices = offense_type, selected = ""),
selectInput("month_freq", "Month", choices = month, selected = ""),
helpText("Note: certain offense and month combinations contain empty data,
and will thus display empty visualizations. This plot is intended to
visualize how frequent crimes in Seattle are for a selected month to
answer the questions concerning what time of month are particularly trending
for crime.")
),
mainPanel(
br(),
textOutput("frequency_title"),
br(),
plotOutput("freq_plot"),
br(),
textOutput("crime_frequency")
)
)
),
tabPanel("Resolved Crime Rates", fluid = TRUE,
sidebarLayout( # layout the page in two columns
sidebarPanel( # specify content for the "sidebar" column
selectInput("crime_in", "Offense Type", choices = offense_type, selected = ""),
selectInput("district_in", "District", choices = districts, selected = ""),
helpText("Note: certain offense and district combinations contain empty data,
and will thus display empty visualizations. This plot is intended to
visualize common crimes in densely populated areas of Seattle to
answer the questions concerning what time of year are most cases
resolved in, relative to the crime and district selected.")
),
mainPanel( # specify content for the "main" column
br(),
textOutput("density_title"),
br(),
plotOutput("density_plot"),
br(),
textOutput("density_analysis")
)
)
),
tabPanel("Documentation", fluid = TRUE,
h2("Project Documentation"),
h4("Created By: Wasif Siddique, Jeremy Chang, Rey Matsunaga, Israel-SixtoSanchez"),
br(),
h3("Project Description"),
p("The data set that we worked with can be found here:",a(href = "https://data.seattle.gov/Public-Safety/Seattle-Police-Department-Police-Report-Incident/7ais-f98f", "Seattle PD data")),
p("This dataset is based on information from the Seattle City Gov website that records incidents based on initial police reports taken by officers when responding to incidents around the city.
We've filtered out the data for 2017 records only."),
p("This data report gives us an insider about how frequently crime is reported over different regions in Seattle. It also presents
visualizations regarding how frequent a certain crime is reported everyday for a specific month, and what the statistics are for the reported crimes to be resolved
by the police."),
br(),
h3("Technical Description"),
p("To create the visualizations we used", a(href = "https://shiny.rstudio.com/", "shiny"), ", ",
a(href = "https://rstudio.github.io/leaflet/", "leaflet for R"), ", and " , a(href = "https://ggplot2.tidyverse.org/", "ggplot"), ".
For the in depth data manipulation we used", a(href = "https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html","dplyr"), "."),
p("The interactive widgets on the side allows the user to select crime types, specify months and location. Therefore the resulting plots and analysis are dynamic,
and will represent the data analysis corresponding to whatever crime type, month or location the user chooses to explore.")
)
)
)
server <- function(input,output) {
# converts month name to indexes
select_month <- reactive({
text <- input$months[1]
if (text == "January" ){
return(1)
} else if (text == "February"){
return(2)
} else if (text == "March") {
return(3)
} else if (text == "April") {
return(4)
} else if (text == "May") {
return(5)
} else if (text == "June") {
return(6)
} else if (text == "July") {
return(7)
} else if (text == "August") {
return(8)
} else if (text == "September") {
return(9)
} else if (text == "October") {
return(10)
} else if (text == "November") {
return(11)
} else if (text == "December") {
return(12)
}
})
# reactive variable for shared data
filtered_table_heatmap <- reactive({
month <- select_month()
crime_data <- filter(data,Summarized.Offense.Description ==
toupper(input$crime_type[1])) %>%
filter(Month == month)
result <- select(crime_data,Date.Reported,Longitude,Latitude,Location,
Month,Year,Occurred.Date.Range.End)
return(result)
})
# outputs a heatmap for the selected crime
output$heatmap <- renderLeaflet({
data_plot <- filtered_table_heatmap()
map <- leaflet(data_plot) %>%
addProviderTiles("CartoDB.Positron") %>%
addHeatmap(lng= ~Longitude, lat= ~Latitude,
blur = 18, max = 0.5 , radius = 15)
return(map)
})
# outputs a title for the map
output$maptitle <- renderText({
text <- HTML(paste0("Heat map for ", tolower(input$crime_type[1]), " in the month of ",
input$months[1]))
return(text)
})
# outputs an analysis for the heatmap visualiztion
output$map_analysis <- renderText({
data_text <- filtered_table_heatmap()
unsolved <- data_text %>%
filter(Occurred.Date.Range.End == "")
unsolved <- NROW(unsolved$Location)
count <- NROW(data_text$Location)
solved <- count - unsolved
summary <-
paste0(
"The heatmap here represents the areas in Seattle where the crime type '", input$crime_type[1], "' is the most common in the
month of ", input$months[1], ". The areas on the map that is more saturated is where '", input$crime_type[1], "' is more frequent.")
report <-
if (count > 0){
paste0(" There were ", count, " reports of '", input$crime_type[1], "' in ", input$months[1], ", of which ", solved, " reports have been
resolved by the Police, and for the rest of ", unsolved, " reports, the investigation is still ongoing.")
} else if( count == 0) {
paste0(" There was no report of ' ", input$crime_type[1], "' in ", input$months[1], ".")
}
text <- HTML(paste0(summary,report))
return(text)
})
#####################
#### SECTION TWO ####
#####################
# Reactive Data Table for the top 5 crimes
filtered_table_bargraph <- reactive({
# Select desired columns
data <- select(data, Summarized.Offense.Description, Month)
month <- crime_month()
district_data <- filter(data, Month == month) %>%
group_by(Summarized.Offense.Description) %>%
summarize(
n = n()
) %>%
arrange(-n)
top_5 <- district_data[1:5, ]
return(top_5)
})
# Create a bar graph with the top 5 crimes on the x-axis and the frequency on the y-axis
output$bargraph <- renderPlot({
data_bar_graph <- filtered_table_bargraph()
bar <- ggplot(data = data_bar_graph,
mapping = aes(x = Summarized.Offense.Description, y = n,
fill = Summarized.Offense.Description)) +
geom_bar(stat = "identity") +
ggtitle("") +
xlab("Crime Type") +
ylab("Number of Incidents") +
guides(fill = guide_legend(title = "Crime Type")) +
theme_bw()
return(bar)
})
# Outputs bar graph title
output$bar_title <- renderText({
text <- HTML(paste0("Top 5 Crimes in the month of ", input$user_month[1] ," in 2017"))
return(text)
})
#Outputs bar graph analysis
output$bar_analysis <- renderText({
data_text <- filtered_table_bargraph()
crime_type <- data_text[, 1]
crime_freq <- data_text[, 2]
text <- HTML(paste0("The top 5 crimes that occured in ", input$user_month[1], " 2017 were ",
tolower(crime_type[1,]), " at ", crime_freq[1,], " occurances, ",
tolower(crime_type[2,]), " at ", crime_freq[2,], " occurances, ",
tolower(crime_type[3,]), " at ", crime_freq[3,], " occurances, ",
tolower(crime_type[4,]), " at ", crime_freq[4,], " occurances, ",
tolower(crime_type[5,]), " at ", crime_freq[5,], " occurances. "
))
return(text)
})
# Outputs bar graph trend
output$bar_trend <- renderText({
text <- HTML("Throughout the year, burglary and car prowl are two of
the most common types of crimes. This is most likely due to the fact that, during
the winter months, with shorter daytimes there is more darkness through out the 24-hr period to conceal
the actions of the criminals.
And during the summer months, there are less people supervising their property, which is likely
because they on vacation.")
return(text)
})
#######################
#### SECTION FOUR #####
#######################
#filtered data
resolved_df <- reactive({
district <- select_district()
filt_df <- data %>%
filter(District.Sector == district) %>%
filter(Summarized.Offense.Description == toupper(input$crime_in)) %>%
select(Summarized.Offense.Description, Month,
Occurred.Date.Range.End, District.Sector) %>%
mutate(Resolved = (Occurred.Date.Range.End != ""))
return(filt_df)
})
#resolved count
rc_count <- reactive({
reactive_data <- resolved_df()
resolved_crimes <- NROW(reactive_data %>%
filter(Resolved == TRUE))
return(resolved_crimes)
})
#unresolved count
urc_count <- reactive({
reactive_data <- resolved_df()
unresolved_crimes <- NROW(reactive_data %>%
filter(Resolved == FALSE))
return(unresolved_crimes)
})
#density plot
output$density_plot <- renderPlot({
reactive_data <- resolved_df()
reactive_res <- rc_count()
reactive_unres <- urc_count()
g <- ggplot(reactive_data, aes(Month)) +
geom_density(aes(fill=factor(Resolved)), alpha=0.7) +
labs(title="",
subtitle=paste0("Percentage of ", input$crime_in, " Crimes reported in ",
input$district_in),
x = "Month",
y="Density (Crime Rate %)",
fill="Case Status") +
scale_x_continuous(breaks = c(seq(1:12)),
label = c("JAN", "FEB", "MAR",
"APR", "MAY", "JUNE",
"JULY", "AUG", "SEPT",
"OCT", "NOV", "DEC")) +
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(name = "Case Status",
labels = list(paste("Unresolved:", reactive_res),
paste("Resolved:", reactive_unres)),
values = c("#f6cac9", "#91a7d0"), #pantones: rosequartz + serenity
guide = guide_legend(reverse=TRUE))
return(g)
})
#plot analysis
output$density_analysis <- renderText({
reactive_res <- rc_count()
reactive_unres <- urc_count()
total <- reactive_unres + reactive_res
para <- paste0("This interactive visualization provides information on the
density of resolved/unresolved crimes in Seattle. It allows
the user to compare different districts and crime types to
determine what month of the year crimes are likely to beresolved.
We can see that throughout the year, there are two peaks consistently
occur from Jan-June, and July-Dec. This particular visualization looks at ",
input$crime_in, " crimes in the ", input$district_in, " district. An important factor
to note, is that of ", input$crime_in, " crimes in the ", input$district_in, " district, only about ",
round(((reactive_unres/total) * 100), 2), "% have been resolved, while ",
round(((reactive_res/total) * 100), 2), "% have remained unresolved in a total of ", total, " crimes."
)
text <- HTML(para)
return(text)
})
# outputs density plot title
output$density_title <- renderText({
text <- HTML("Resolved Crime Rate Density by Crime & District")
return(text)
})
# Reactive function to select correct district
select_district <- reactive({
text <- input$district_in[1]
if (text == "U-District") {
return("U")
} else if (text == "West Seattle") {
return("W")
} else if (text == "Greater Ballard") {
return("J")
} else if (text == "North Seattle") {
return("N")
}
})
#######################
#### SECTION THREE ####
#######################
# Get Month
select_month_freq <- reactive({
text <- input$month_freq[1]
if (text == "January" ){
return(1)
} else if (text == "February"){
return(2)
} else if (text == "March") {
return(3)
} else if (text == "April") {
return(4)
} else if (text == "May") {
return(5)
} else if (text == "June") {
return(6)
} else if (text == "July") {
return(7)
} else if (text == "August") {
return(8)
} else if (text == "September") {
return(9)
} else if (text == "October") {
return(10)
} else if (text == "November") {
return(11)
} else if (text == "December") {
return(12)
}
})
# Get data
filtered_table_freq_plot <- reactive({
data <- filter(data, Summarized.Offense.Description == toupper(input$crime_freq_type) & Month == select_month_freq() & Year == 2017)
result <- select(data, Summarized.Offense.Description,Date.Reported, Month, Year)
return(result)
})
# Render frequncy Plot over time for specified crime
output$freq_plot <- renderPlot({
library(RColorBrewer)
result <- filtered_table_freq_plot() # Get Data
result$Date <- as.character(as.Date(as.character(as.POSIXct(result$Date.Reported,
format = "%m/%d/%Y %H:%M:%S %p"))))
# Plot
x <- ggplot(result) +
geom_bar(mapping = aes(x = Date), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0.6)) +
scale_color_brewer(palette = "Set3") +
labs(title= "",
x = "Month",
y="Frequency of Crime")
return(x)
})
# Outputs crime frequency analysis
output$crime_frequency <- renderText({
result <- filtered_table_freq_plot()
result$Date <- as.character(as.Date(as.character(as.POSIXct(result$Date.Reported,
format = "%m/%d/%Y %H:%M:%S %p"))))
result_grouped <- group_by(result, Date) %>%
summarize(
n = n()
) %>%
arrange(-n)
highest_date <- result_grouped[1, 1]
highest <- result_grouped[1, 2]
text <- HTML(paste0("This frequency plot shows a visualization of how frequent ",input$crime_freq_type ," was
for the month of ",input$month_freq[1],". The highest record of ", input$crime_freq_type, " that happened in " ,
input$month_freq[1], "is ", highest, " which occurred on ", highest_date, "."))
return(text)
})
# Outputs frequency graph title
output$frequency_title <- renderText({
text <- HTML(paste0("Bar Graph for ", input$crime_freq_type, " over each day in ",input$month_freq[1],"."))
})
crime_month <- reactive({
text <- input$user_month[1]
if (text == "January" ){
return(1)
} else if (text == "February"){
return(2)
} else if (text == "March") {
return(3)
} else if (text == "April") {
return(4)
} else if (text == "May") {
return(5)
} else if (text == "June") {
return(6)
} else if (text == "July") {
return(7)
} else if (text == "August") {
return(8)
} else if (text == "September") {
return(9)
} else if (text == "October") {
return(10)
} else if (text == "November") {
return(11)
} else if (text == "December") {
return(12)
}
})
}
# Create a new `shinyApp()` using the above ui and server
shinyApp(ui = ui, server = server)