-
Notifications
You must be signed in to change notification settings - Fork 0
/
hartford-police.Rmd
252 lines (178 loc) · 7.45 KB
/
hartford-police.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
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
---
title: "Reported Crime Incidents in Hartford, CT"
author: "By Jasmine Dumas"
output:
flexdashboard::flex_dashboard:
theme: yeti
orientation: columns
vertical_layout: fill
social: [ "twitter", "facebook", "menu" ]
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DT) # dev version devtools::install_github("rstudio/DT")
library(leaflet)
library(readr)
library(lubridate)
library(ggplot2)
library(plotly)
library(shiny)
library(RCurl)
library(RSocrata)
```
Sidebar {.sidebar}
=====================================
Explore reported incidents of crime (with the exception of sexual assaults, which are excluded by statute) that occurred in the City of Hartford from 2005 to the present, minus the most recent ten days from the [Hartford Open Data Portal](https://data.hartford.gov/).
```{r, shiny_in}
# shiny inputs defined here
hr()
selectInput("year", "Select Year: ", choices =c(2005, 2006, 2007,
2008, 2009, 2010,
2011, 2012, 2013,
2014, 2015, 2016, 2017) ,
selected = 2017)
hr()
```
Made for fun & interest by [Jasmine Dumas](http://jasdumas.github.io/).
Explore
=====================================
```{r, message=FALSE, warning=FALSE, include=FALSE}
# URL = "https://data.hartford.gov/api/views/889t-nwfu/rows.csv?accessType=DOWNLOAD"
# x <- getURL(URL, ssl.verifypeer = FALSE)
# out <- read_csv("https://data.hartford.gov/Public-Safety/Police-Incidents-01012005-to-Current/889t-nwfu")
#data_in <- read_csv("Police_Incidents_01012005_to_Current.csv")
# get the data from data.hartford.gov as the API url
data_in <- read.socrata("https://data.hartford.gov/Public-Safety/Police-Incidents-01012005-to-Current/889t-nwfu")
##############################
# clean up lat and longitude #
##############################
# removes the parenthesis
s = gsub(".*\\((.*)\\).*", "\\1", data_in$geom)
# extract and split each f the tuple latitude and longitude
lat_splat = sapply(s, function(x) strsplit(x, ",")[[1]][1])
lon_splat = sapply(s, function(x) strsplit(x, ",")[[1]][2])
# convert to data.frame
lat <- as.data.frame(lat_splat)
lon <- as.data.frame(lon_splat)
# convert to factor to numeric
options(digits=15)
lat2 <- sapply(lat, function(x) as.numeric(levels(x))[x])
lat2 <- as.data.frame(lat2)
lon2 <- sapply(lon, function(x) as.numeric(levels(x))[x])
lon2 <- as.data.frame(lon2)
# add to main data frame
data_in$lat <- lat2
data_in$lon <- lon2
#################
# clean up date #
#################
class(data_in$Date)
data_in$Date <- mdy(data_in$Date)
data_in$year <- year(data_in$Date)
#####################
# clean up category #
#####################
## grep out leader symbol
data_in$UCR_1_Category <- gsub("[0-9]","", data_in$UCR_1_Category)
data_in$UCR_1_Category <- gsub("[[:punct:]]","", data_in$UCR_1_Category)
#######################################
# subset data by date for explore tab #
#######################################
## so many points!! - use sample for now
data_in <- as.data.frame(data_in)
data_sample <- reactive({
data_sample <- data_in[which(data_in$year == input$year), ]
data_sample
})
```
### Map
```{r, map}
renderLeaflet({
m <- leaflet(data=data_sample()) %>%
setView(lng = -72.685097, lat = 41.763710, zoom = 11) %>%
addProviderTiles("Stamen.Toner") %>%
addMarkers(lng=~lon$lon_splat, lat=~lat$lat_splat,
popup = ~as.character(UCR_2_Description),
clusterOptions = markerClusterOptions())
m
})
```
### Data Table
```{r, dt}
DT::renderDataTable({
DT::datatable(data_sample(), options = list(bPaginate = TRUE,
pageLength = 15
))
})
```
Data Visualization {.storyboard}
=====================================
### **Crime by Neighborhood:** Which neighboorhoods have had the most reported incidents?
```{r, viz_display2}
category <- dplyr::count(data_in, UCR_1_Category)
neighborhood <- dplyr::count(data_in, UCR_1_Category, Neighborhood)
g2 <- ggplot(neighborhood, aes(x=reorder(Neighborhood, n), y = n, fill=factor(UCR_1_Category))) +
geom_bar(stat="identity") +
theme(axis.text.x =element_blank()) +
theme(legend.title=element_blank()) +
theme_bw() +
coord_flip() +
labs(title="Overall Count of Incidents by Crime Type & Neighborhood",
x = "", y = "Count")
ggplotly(g2)
```
### **Decrease in Reported Crimes:** Has the amount of reported crime incidents been decreasing over the years?
```{r}
safe <- dplyr::count(data_in, year)
p1 <- ggplot(safe, aes(x = year, y = n)) +
geom_line(color = "dodgerblue") +
geom_point(color = "dodgerblue") +
theme_bw() +
labs(title = "Annual Amount of Reported Incidents", x = "", y = "Count")
ggplotly(p1)
```
### **Decrease in Violent Crimes:** Can lower amounts of reported crime incidents be related to fewer violent crimes?
```{r}
violent <- dplyr::count(data_in, UCR_1_Category, year)
really_violent <- c(" AGGRAVATED ASSAULT", " ROBBERY", " HOMICIDE", " LARCENY")
violent2 <- violent[violent$UCR_1_Category %in% really_violent,]
vi <- ggplot(data=violent2, aes(x=year, y=n, group=UCR_1_Category, color=UCR_1_Category)) +
geom_line() +
geom_point() +
theme(legend.position="none") +
theme_bw() +
labs(title = "Annual Amount of Reported Violent Crimes", x = "", y = "Count")
ggplotly(vi)
```
### **Guns in Hartford:** As a National issue about gun control and safety, [Conneticut has enacted the toughest restrictions on purchasing weapons and ammunition](https://en.wikipedia.org/wiki/Gun_laws_in_Connecticut) since 2012.
```{r}
gun_related <- c(" SHOOTING", " SHOTS FIRED", " SHOTS FIRED CONFIRMED", " SHOTS FIRED UNCONFIRMED")
gun_df <- dplyr::count(data_in, UCR_1_Category, year)
gun_df2 <- gun_df[gun_df$UCR_1_Category %in% gun_related, ]
gu <- ggplot(gun_df2, aes(x = UCR_1_Category)) +
geom_bar(fill = "#536267") +
theme_bw() +
labs(title = "Amount of Gun-Related Reported Crimes", x = "", y = "Count")
ggplotly(gu)
```
### **The Drug Epidemic:** [Connecticut](http://overdose.trendct.org/) continues to see a sharp rise in heroin and other opioid-related deaths.
```{r}
drugs <- c(" DRUG OFFENSES")
drugs_df <- dplyr::count(data_in, UCR_1_Category, Neighborhood)
drugs_df <- drugs_df[drugs_df$UCR_1_Category %in% drugs, ]
##
d <- ggplot(drugs_df, aes(x=reorder(Neighborhood, n), y = n)) +
geom_bar(stat = "identity", fill = "#6699CC") +
theme_bw() +
coord_flip() +
labs(title = "Amount of Drug-Related Reported Crimes", y = "", x = "")
ggplotly(d)
```
Details
=====================================
* This dataset reflects reported incidents of crime (with the exception of sexual assaults, which are excluded by statute) that occurred in the City of Hartford from 2005 to the present, minus the most recent ten days. Data is extracted from the City of Hartford Police Department's CrimeView database on a daily basis. Should you have questions about this dataset, you may contact the Crime Analysis Division of the Hartford Police Department at 860.757.4020 or policechief@Hartford.gov.
* Full Disclaimer available at [https://data.hartford.gov/Public-Safety/Police-Incidents-01012005-to-Current/889t-nwfu](https://data.hartford.gov/Public-Safety/Police-Incidents-01012005-to-Current/889t-nwfu)
* [Socrata Hartford Police Codes (Download)](https://data.hartford.gov/api/views/889t-nwfu/files/BOGvjBIm4--AIkVZZIBXBljNj-PrylsI5BxLEEQ4Lms?download=true&filename=Socrata%20HPD%20UCR%20Codes.pdf)
* Project inspiration: [Real-time Dallas Police Calls](https://trestletech.shinyapps.io/dallas-police/)