-
Notifications
You must be signed in to change notification settings - Fork 1
/
GV_Flex_Dashboard.Rmd
341 lines (264 loc) · 15.9 KB
/
GV_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
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
---
title: "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)
# library(RCurl)
# setwd()
# options(rsconnect.check.certificate = FALSE)
source("Data_Extraction/Gun_Violence_Scraping.R")
source("Data_Extraction/Open_Philly_Data.R")
```
United States GV
=====================================
Column {.sidebar}
-----------------------------------------------------------------------
```{r First Input Panel}
dateInput(inputId = "startdate", label = "Select Start Date", value = "2013-01-01",min = "2013-01-01", format = "yyyy-mm-dd")
dateInput(inputId = "enddate", label = "Select End Date", value = "2022-12-31",min = "2013-01-01", format = "yyyy-mm-dd")
radioButtons(inputId = "victimtype", label = "Select Victim Type", selected = "Died", choices = c("Died", "Injured"))
```
Column {.tabset .tabset-fade}
-----------------------------------------------------------------------
### **GV Count Map**
```{r US Count Map,echo=FALSE}
renderHighchart({
state_gv.df <- state_gv.df %>%
filter(date >= input$startdate & date <= input$enddate) %>%
group_by(STATE_ABBR) %>%
tally()
hcmap("countries/us/us-all", value = "n", data = state_gv.df,
joinBy = c("hc-a2", "STATE_ABBR"), name = "Mass Shooting Count:",
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 0, valuePrefix = "", valueSuffix = "")) %>%
hc_chart(zoomType = "xy") %>%
hc_chart(backgroundColor = "white") %>%
hc_legend(enabled = F) %>%
# hc_add_theme(hc_theme_db()) %>%
hc_title(text = paste0("Number of Gun Violence Incidents: ", substr(floor_date(input$startdate, "month"),1,7), " ",
" to ", substr(floor_date(input$enddate, "month"),1,7)), align = "left", color = "white") %>%
hc_exporting(enabled = TRUE, filename = "GV_Map") %>%
hc_credits(enabled = TRUE, text = "Source: https://www.gunviolencearchive.org/, https://www.massshootingtracker.org/") %>%
hc_colorAxis(minColor = "white", maxColor = "firebrick", type = "logarithmic")
})
```
### **Normalized GV Map**
```{r Normalized US Map,echo=FALSE}
renderHighchart({
state_gv.df <- state_gv.df %>%
filter(date >= input$startdate & date <= input$enddate) %>%
group_by(STATE_ABBR) %>%
tally()
state_gv_norm.df <- merge.data.frame(x = state_gv.df, y = state_pop, by = "STATE_ABBR")
state_gv_norm.df$Normalization <- (state_gv_norm.df$n/state_gv_norm.df$Population) * 100000
hcmap("countries/us/us-all", data = state_gv_norm.df, value = "Normalization",
joinBy = c("hc-a2", "STATE_ABBR"), name = "Shootings per 100k Population:",
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 2, valuePrefix = "", valueSuffix = "")) %>%
hc_chart(zoomType = "xy") %>%
hc_chart(backgroundColor = "white") %>%
hc_legend(enabled = F) %>%
# hc_add_theme(hc_theme_db()) %>%
hc_title(text = paste0("GVA Mass Shooting Population Normalized Incidence: ", substr(floor_date(input$startdate, "month"),1,7), " ",
" to ", substr(floor_date(input$enddate, "month"),1,7)), align = "left", color = "white") %>%
hc_subtitle(text = "Population Normalized", align = "left", color = "white") %>%
hc_exporting(enabled = TRUE, filename = "GV_Map") %>%
hc_credits(enabled = TRUE, text = "Source: https://www.gunviolencearchive.org/, https://www.massshootingtracker.org/") %>%
hc_colorAxis(minColor = "white", maxColor = "darkorange", type = "logarithmic")
})
```
### **Deaths vs Injuries**
```{r US Victim Type Map,echo=FALSE}
renderHighchart({
ifelse(input$victimtype == "Died",
victim_gv.df <- us_gv.df %>%
filter(date >= input$startdate & date <= input$enddate) %>%
group_by(STATE_ABBR) %>%
tally(killed),
victim_gv.df <- us_gv.df %>%
filter(date >= input$startdate & date <= input$enddate) %>%
group_by(STATE_ABBR) %>%
tally(injured)
)
# us_gv.df %>%
# filter(date >= input$startdate & date <= input$enddate) %>%
# group_by(STATE_ABBR) %>%
# tally(killed)
hcmap("countries/us/us-all", value = "n", data = victim_gv.df,
joinBy = c("hc-a2", "STATE_ABBR"), name = paste0("No. ", input$victimtype, ":"),
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 0, valuePrefix = "", valueSuffix = "")) %>%
hc_chart(zoomType = "xy") %>%
hc_chart(backgroundColor = "white") %>%
hc_legend(enabled = F) %>%
# hc_add_theme(hc_theme_db()) %>%
hc_title(text = paste0(input$victimtype ," In Mass Shootings: ", substr(floor_date(input$startdate, "month"),1,7), " ",
" to ", substr(floor_date(input$enddate, "month"),1,7)), align = "left", color = "white") %>%
hc_exporting(enabled = TRUE, filename = "GV_Map") %>%
hc_credits(enabled = TRUE, text = "Source: https://www.gunviolencearchive.org/, https://www.massshootingtracker.org/") %>%
hc_colorAxis(minColor = "white", maxColor = "steelblue", type = "logarithmic")
})
```
Column {data-width=350}
-----------------------------------------------------------------------
### **Monthly GV Deaths**
```{r US Monthly Bar Plot,echo=FALSE}
GVA_Month
```
### **Yearly GV Deaths**
```{r US Yearly Bar PLot,echo=FALSE}
GVA_Year
```
Philadelphia, PA GV
=====================================
Column {.sidebar}
-----------------------------------------------------------------------
```{r Second Input Panel}
dateInput(inputId = "startdate2", label = "Select Start Date", value = "2013-01-01",min = "2013-01-01", format = "yyyy-mm-dd")
dateInput(inputId = "enddate2", label = "Select End Date", value = "2022-12-31",min = "2013-01-01", 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)) %>%
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 Gun Violence & Mass Shooting Data Tracker dashboard. This dashboard consists of various data displays fed from different data streams.
The **United States GV** tab shows nation-wide data scraped from both the **[Gun Violence Archive](https://www.gunviolencearchive.org/) (GVA)** and the **[Mass Shooting Tracker](http://massshootingtracker.org/) (MST)** and combines them to ensure as many incidents are tracked and verified as possible.**
<span style="color:red">**The MST database has been reinstated at a [new web address](http://massshootingtracker.org/about/) as of 2020. This unfortunately does not include data from the old database as of yet and the new data is not being incorporated currently. The data from the original MST database was downloaded and frozen. Data displayed from the original MST only encompasses incidents from 2013 to October 2019. All other data beyond is reliant on GVA.**</span>
The **Philadelphia PA GV** tab 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**
**United States GV**:
The following liberties were taken and definitions made when compiling this dataset:
- The District of Columbia (D.C.) as a standalone district was excluded due to the high incidence per capita. In the normalization graphs D.C. had a density of over 5 which heavily skewed the graph.
- A “Mass Shooting” is being defined as “a single outburst in which four or more people are shot.” For reference, the FBI defines a "mass murder" as "four or more murdered during an event with no "cooling-off period" between the murders." The definitions have changed over the years and the U.S. acknowledges that there is no agreed upon definition at present.
- Only data taken from 2013 to the present is accounted for due to a lack of resources from before this year and varying definitions for what constitutes a mass shooting as time has gone on since.
**Philadelphia PA GV**:
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**
**United States GV**:
Data-normalization was conducted by dividing the number of mass shootings by the total population of the state, multiplied by 100,000. This can therefore be read as “n” incidents per 100,000 people in a given state. This method is in accordance with generally accepted practices as shown in **[MIT’s Department of Urban Studies and Planning](http://web.mit.edu/11.520/www/labs/lab5/normalize.html)**. At the moment, this only takes into account the given years in totality, future efforts will look into incorporating date toggles to limit scope by time.
**Philadelphia PA GV**:
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**
**United States GV**:
GV incidence reports were taken from a combination of:
- **[Gun Violence Archive](https://www.gunviolencearchive.org/) (GVA)**
- **[Mass Shooting Tracker](http://massshootingtracker.org/) (MST)**
U.S. population estimates were taken from the **[2018 U.S. Census totals]( https://www.census.gov/data/datasets/time-series/demo/popest/2010s-state-total.html#par_textimage_500989927)**.
**Philadelphia PA GV**:
- **[Open Data Philly](https://www.opendataphilly.org/dataset/shooting-victims)**
### **Additional Information**
** While the decision was made to use both the GVA and MST databases, there is some controversy over MST use due to crowd sourced, unfunded Reddit input. However, each entry by users is curated by the sub-forum moderators and require at least 2 sources to link each event to. Since GVA may be considered a gold standard, but contain less information due to rigidity, it was decided to join the two databases together and ensure nothing from GVA was missing, but that MST could supplement. It was found that on combining the databases the total number of entries equaled that of MST alone which indicates that MST picked up every entry of GVA with some additional entries likely due to greater resources and looser definitions.
** 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.