-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex_dashboard.Rmd
122 lines (95 loc) · 4.31 KB
/
flex_dashboard.Rmd
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
---
title: "Sex-Related, Weapon-Related & Drug-Related Crimes in NYC"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r flex_libraries}
library(flexdashboard)
library(plotly)
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(sp)
library(maps)
library(chron)
library(lubridate)
library(rvest)
library(tools)
library(geojsonio)
```
```{r flex_data}
sex_drug_weapons = readRDS(file = "./datasets/sex_drug_weapons.rds")
###Dataset for 2017 with addresses
crimes_df = readRDS(file = "./datasets/all_sex_drug_weapon_zip_2017.rds")
```
Column {data-width=550}
-----------------------------------------------------------------------
### Chart A: Distribution for sex-related, drug-related and weapon-related felonies for 2017 by Zipcode
```{r flex_chatA}
map_json = geojson_read("./datasets/nyc_zip_map.geojson", what = "sp")
crimes_df = crimes_df %>%
mutate(zipcode = str_extract(zip, "[0-9][0-9][0-9][0-9][0-9]"),
boro_nm = toTitleCase(boro_nm))
crime_group_sum = crimes_df %>% group_by(zipcode) %>% summarize(number = n())
map_json@data <-
map_json@data %>% left_join(crime_group_sum, by = c("postalCode" = "zipcode")) %>%
mutate(number = as.numeric(number)) %>%
mutate(number = if_else(is.na(number), 0, number))
# create color palette with colorNumeric()
nyc_pal <- colorNumeric("Blues", domain = map_json@data$number)
map_json %>%
leaflet() %>%
addProviderTiles("OpenStreetMap.BlackAndWhite", options = providerTileOptions(opacity = 0.4)) %>%
setView(lat = 40.7, lng = -73.9, zoom = 11) %>%
addPolygons(weight = 1, fillColor = ~ nyc_pal(number),
color = "gray",
fillOpacity = 3, opacity = 1,
smoothFactor = 0.2,
# add labels that display mumber
label = ~ paste(postalCode, borough, ':', number, sep = '\n'),
# highlight polygons on hover
highlight = highlightOptions(weight = 2, color = "black",
bringToFront = TRUE)) %>%
addLegend(pal = nyc_pal,
values = c(0:305),
opacity = 0.75,
title = "Number of incident crimes ",
position = "topleft")
```
Column {data-width=450}
-----------------------------------------------------------------------
### Chart B: Distribution of victims of sex and weapon-related felonies by age group 2014 - 2017
```{r flex_chartB}
sex_drug_weapons %>% filter(!is.na(vic_age_group), vic_age_group != "unknown") %>%
mutate(vic_age_group = str_c(vic_age_group, " years")) %>%
filter(crime_group == "Sex-Related" | crime_group == "Weapon-Related") %>%
group_by(crime_group, vic_age_group) %>%
summarize(number = n()) %>%
mutate(text_label = paste("Age group = ", vic_age_group, "\nCrime Group = ", crime_group, "\nCount = ", number)) %>%
plot_ly(x = ~ crime_group, y = ~ number, color = ~ vic_age_group, type = "bar", text = ~ text_label) %>%
layout(yaxis = list(title = 'Count'), xaxis = list(title = 'Crime Group'))
```
### Chart C: Distribution of victims of sex, drug and weapon-related felonies by race/ethnicity from 2014 - 2017
```{r flex_chartC}
season_df = sex_drug_weapons %>%
mutate(quarter = as.numeric(quarter(cmplnt_fr_dt)),
month = months(cmplnt_fr_dt),
month = forcats::fct_relevel(month, c("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December")),
day = weekdays(cmplnt_fr_dt),
day = forcats::fct_relevel(day, c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")),
cmplnt_fr_tm = lubridate::hms(as.character(cmplnt_fr_tm)),
hour = hour(cmplnt_fr_tm)) %>% select(-cmplnt_fr_tm)
#### Filtering out unknown victim race
p = season_df %>% filter(vic_race != "unknown") %>%
group_by(vic_race, boro_nm) %>% summarize(number = n()) %>%
ggplot(aes(x = reorder(vic_race, number), y = number, fill = vic_race)) + geom_col() +
theme(legend.position = "left", axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()) +
labs(y = "Total Number") + viridis::scale_fill_viridis(discrete = TRUE,
name = "Race/Ethnicity")
ggplotly(p)
```