-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhiladelphia_GV.Rmd
181 lines (145 loc) · 7.19 KB
/
Philadelphia_GV.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
---
title: "Philadelphia Gun Violence Data Tracker"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
theme: readable
runtime: shiny
---
```{r setup, include=FALSE}
# Load Applicable Libraries ====================================================
library(shiny)
library(tidyverse)
library(rvest)
library(purrr)
library(lubridate)
library(RColorBrewer)
library(highcharter)
library(flexdashboard)
library(leaflet)
library(DT)
library(readr)
source("Open_Philly_Data.R")
```
```{r input_demo, eval = FALSE}
input <- list(
enddate2 = "2022-12-31",
startdate2 = "2013-01-01"
)
```
Philadelphia, PA GV
=====================================
Column {.sidebar}
-----------------------------------------------------------------------
```{r Second Input Panel}
dateInput(inputId = "startdate2", label = "Select Start Date", value = "2013-01-01", format = "yyyy-mm-dd")
dateInput(inputId = "enddate2", label = "Select End Date", value = "2022-12-31", format = "yyyy-mm-dd")
```
Column
-----------------------------------------------------------------------
### **Philadelphia Shooting Incidents Map**
```{r Philly Map,echo=FALSE}
renderLeaflet({
# Tally the incident numbers for rounded long and lat values
philly.gv_round <- philly.gv %>%
filter(!is.na(lat)) %>% # Remove NA values which sum up in the end
filter(date_ >= input$startdate2 & date_ <= input$enddate2) %>%
group_by(lat, lng) %>%
tally()
leaflet(data = philly.gv_round) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng = -75.16,
lat = 39.95,
zoom = 13) %>%
addCircles(lng = ~lng, lat = ~lat, weight = 1,
radius = ~sqrt(n)*50,
popup = ~paste(sep = "<br/>", "<b> Number of Incidents:</b>", as.character(n)),
fillColor = ~pal(n), stroke = NA, fillOpacity = 0.8
) %>%
addLegend("bottomright", pal = pal, values = ~n,
title = "Shooting Incidents (n)",
labFormat = labelFormat(prefix = ""),
opacity = 1
)
})
```
-----------------------------------------------------------------------
### **Philadelphia Shooting Incidents - Tabular Data**
```{r Philly Table,echo=FALSE}
philly.gv %>%
select("Code" = code, "Date" = date_, "Race" = race, "Sex" = sex, "Age" = age,
"Wound Location" = wound, "Office Involved" = officer_involved,
"Offender Deceased" = offender_deceased,
"Offender Injured" = offender_injured, "Location" = location,
"Latino" = latino, "Inside" = inside, "Outside" = outside,
"Fatal" = fatal) %>%
DT::datatable(class = 'cell-border compact hover stripe',
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 1: ', htmltools::em('Shooting incidents in Philadelphia, PA.')
),
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('csv')
)
)
```
Column
-----------------------------------------------------------------------
### **Demographic Percentages**
```{r Philly Perc Plot,echo=FALSE}
renderHighchart({
philly.gv_perc <- philly.gv %>%
filter(date_ >= input$startdate2 & date_ <= input$enddate2) %>%
mutate("% Latino" = round(sum(latino, na.rm = T)/nrow(philly.gv),4)*100,
"% Black" = round(sum(race == "B" | race == "b", na.rm = T)/nrow(philly.gv),4)*100,
"% White" = round(sum(race == "W" | race == "w", na.rm = T)/nrow(philly.gv),4)*100,
"% Inside" = round(sum(inside, na.rm = T)/nrow(philly.gv),4)*100,
"% Outside" = round(sum(outside, na.rm = T)/nrow(philly.gv),4)*100,
"% Fatal" = round(sum(fatal, na.rm = T)/nrow(philly.gv),4)*100,
"% Officer Involved" = round(sum(officer_involved == "Y", na.rm = T)/nrow(philly.gv),4)*100)
philly.gv_perc <- reshape2::melt(philly.gv_perc[1,26:32])
colnames(philly.gv_perc) <- c("Variable", "Percentage")
hchart(philly.gv_perc, "bar", hcaes(x = Variable, y = Percentage)) %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_tooltip(pointFormat = paste('{point.y}%')) %>%
hc_legend(enabled = F)
})
```
-----------------------------------------------------------------------
### **Frequency Over Time**
```{r Philly Time Plot,echo=FALSE}
renderHighchart({
philly.gv_n <- philly.gv %>%
filter(date_ >= input$startdate2 & date_ <= input$enddate2)
philly.gv_n %>%
group_by(floor_date(date_, "month")) %>%
tally() %>%
hchart("line", hcaes(x = `floor_date(date_, "month")`, y = n)) %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_tooltip(pointFormat = paste('{point.y}')) %>%
hc_xAxis(title = "Time") %>%
hc_legend(enabled = F)
})
```
User Guide
=====================================
Column
-----------------------------------------------------------------------
### **Introduction**
Welcome to the Philadelphia Gun Violence dashboard.
The dashboard scrapes data from **[Open Data Philly](https://www.opendataphilly.org/dataset/shooting-victims)** which is self-branded as "the official open data repository for the City." This tab does not look into "mass shooting" events and instead looks at all reported shooting incidents. Use the **Philadelphia Shooting Incidents Map** to visually explore the frequency of shootings in different areas of the city. Changes to the date filters in the side column will update accordingly. Larger circles with brighter colors indicate greater frequency of incidents.
The **Demographic Percentages** bar plot illustrates different percentage characteristics related to the anatomy of shooting incidents. Displayed characteristics are in relation to victims. This plot, as well as the **Frequency Over Time**, will update with changes to the date filters on the side column, however the **Philadelphia Shooting Incidents – Tabular Data** table will not. Users are welcome to search by specific criteria as well as use the “CSV” button to export a .csv file containing all data. _Please note the data will take time to export and the dashboard may freeze, do not refresh the page during this time_.
This tracker is not meant to display bias, and merely serves to give a view of the factual data that exists.
### **Assumptions**
The Philadelphia map takes different considerations into account and does not restrict to only "mass shooting" events, specifically "city-wide shooting victims, including Police Officer-involved shootings."
### **Techniques**
To decrease map congestion and load times, geospacial coordinates were rounded to the nearest hundredth to group nearby incidents together. Therefore the location is a rough estimate.
Column
-----------------------------------------------------------------------
### **Resources**
- **[Open Data Philly](https://www.opendataphilly.org/dataset/shooting-victims)**
### **Additional Information**
** At the moment the dashboard only reports on descriptive statistics and is not an indicator for predictive analytics.
** This tracker is not meant to display bias, and merely serves to give a view of the factual data that exists. It is **not** the author's intention to promote or incentivize any type of thinking or political view.