library(leaflet)
package 㤼㸱leaflet㤼㸲 was built under R version 4.0.4Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
-- Attaching packages -------------------------------------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.3.3 v purrr 0.3.4
v tibble 3.1.0 v dplyr 1.0.4
v tidyr 1.1.3 v stringr 1.4.0
v readr 1.4.0 v forcats 0.5.1
package 㤼㸱tidyr㤼㸲 was built under R version 4.0.4-- Conflicts ----------------------------------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(lubridate)
package 㤼㸱lubridate㤼㸲 was built under R version 4.0.4
Attaching package: 㤼㸱lubridate㤼㸲
The following objects are masked from 㤼㸱package:base㤼㸲:
date, intersect, setdiff, union
#library(htmltools)
library(broom)
library(plotly)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Attaching package: 㤼㸱plotly㤼㸲
The following object is masked from 㤼㸱package:ggplot2㤼㸲:
last_plot
The following object is masked from 㤼㸱package:stats㤼㸲:
filter
The following object is masked from 㤼㸱package:graphics㤼㸲:
layout
library(DT)
leaflet() %>%
addTiles()
NA
In this analysis we will take a look at mapping the center of the continental United States as well as where mass shootings occur in the United States.
First we will begin with locating the center of the continental United States. According to Wikipedia the center is located at 44 58’02“N, 103 46’18”W. The center is located just north of Belle Fourche, South Dakota.
44.68 -103.86
58/60
[1] 0.9666667
46/60
[1] 0.7666667
leaflet() %>%
addTiles() %>%
setView(lat = 44.68, lng = -103.86, zoom = 11) %>%
addMarkers(lat = 44.68, lng = -103.86)
NA
NA
NA
NA
shootings <- read_csv("https://docs.google.com/spreadsheets/d/1b9o6uDO18sLxBqPwl_Gh9bnhW-ev_dABH83M5Vb5L8o/export?format=csv", na = "-")
Duplicated column names deduplicated: 'location' => 'location_1' [8]
-- Column specification -------------------------------------------------------------------------------------
cols(
.default = col_character(),
fatalities = col_double(),
injured = col_double(),
total_victims = col_double(),
age_of_shooter = col_double(),
latitude = col_double(),
longitude = col_double(),
year = col_double()
)
i Use `spec()` for the full column specifications.
Next we will view where shooting incidents have taken place throughout the continental United States. The dot size indicates the size of the incident. The map shows that mass shootings are widely spread in the United States.
shootings %>%
leaflet() %>%
addTiles() %>%
setView(lat = 44.68, lng = -103.86, zoom = 4) %>%
addCircleMarkers(stroke = F, fillOpacity = .6, radius = ~log(total_victims))
Assuming "longitude" and "latitude" are longitude and latitude, respectively
There is a median of 10 total victims per incident as well as a median of 6 total fatalities per incident.
shootings %>%
drop_na(total_victims) %>%
summarize(median_total_victims = median(total_victims))
shootings %>%
drop_na(fatalities) %>%
summarize(median_fatalities = median(fatalities))
shootings %>%
plot_ly(x = ~fatalities) %>%
add_histogram(nbinsx = 40)
NA
The following data table is a list of incidents and includes such information as what happened, where it happened, when it happened, how many total victims, total fatalities, and total number of injured. Our most recent mass shooting occurred in Boulder, Colorado on March 22, 2021.
shootings %>%
select(case, location, date, fatalities, total_victims, injured) %>%
datatable()
In the following graphs, you can see who is the perpetrator in these incidents.These grapsh show that mass shootings are mainly committed by white, middle-aged, males.
shootings %>%
drop_na(race) %>%
mutate(race = as_factor(race)) %>%
mutate(race = fct_collapse(race,
White = c("White", "white"),
Black = c("Black", "black"),
Other = c("Other", "unclear"))) %>%
count(race)
shootings %>%
drop_na(race) %>%
mutate(race = as_factor(race)) %>%
mutate(race = fct_collapse(race,
White = c("White", "white"),
Black = c("Black", "black"),
Other = c("Other", "unclear"))) %>%
mutate(gender = fct_collapse(gender,
Female = c("Female", "F"),
Male = c("Male", "M"),
Other = c("Male & Female"))) %>%
plot_ly(x = ~gender, y = ~race) %>%
add_histogram2dcontour()
NA
shootings %>%
plot_ly(x = ~age_of_shooter, y = ~gender) %>%
mutate(gender = fct_collapse(gender,
Female = c("Female", "F"),
Male = c("Male", "M"),
Other = c("Male & Female"))) %>%
add_histogram2dcontour()
Next we will show how many injuries versus fatalities there were in these shootings.
fatalities_data <- shootings %>% # start with the shootings data
filter(fatalities > 3) %>%
group_by(year) %>% # we're going to count by year
summarize(count = n(), fatalities = sum(fatalities)) %>% # get the total number of fatalities per year
mutate(fatalities_per_incident = fatalities/count) # divide by the number of shooting incidents
fatalities_data
fatalities_per_incident_model <- lm(fatalities_per_incident ~ year, data = fatalities_data)
fatalities_per_incident_model
Call:
lm(formula = fatalities_per_incident ~ year, data = fatalities_data)
Coefficients:
(Intercept) year
-67.11301 0.03765
tidy(fatalities_per_incident_model)
glance(fatalities_per_incident_model)
injuries_data <- shootings %>% # start with the shootings data
filter(injured > 3) %>%
group_by(year) %>% # we're going to count by year
summarize(count = n(), injured = sum(injured)) %>% # get the total number of fatalities per year
mutate(injuries_per_incident = injured/count)
injuries_data
injuries_per_incident_ <- lm(injuries_per_incident ~ year, data = injuries_data)
injuries_per_incident_
Call:
lm(formula = injuries_per_incident ~ year, data = injuries_data)
Coefficients:
(Intercept) year
-1558.1211 0.7875
tidy(injuries_per_incident_)
glance(injuries_per_incident_)
You can see in the next two graphs that the number of fatalites and the number of injured has risen slightly through the years.
fatalities_data %>%
plot_ly(x = ~year,
y = ~fatalities,
hoverinfo = "text",
text = ~paste("Fatalities",
round(fatalities_per_incident, 1), "<br>", "Year: ", year)) %>%
add_markers(showlegend = F, ) %>%
add_lines(y = ~fitted(fatalities_per_incident_model)) %>%
layout(title = "Number of Fatalities",
xaxis = list(title = "Year"),
yaxis = list(title = "Fatalities"))
NA
injuries_data %>%
plot_ly(x = ~year,
y = ~injured,
hoverinfo = "text",
text = ~paste("Number of Injured ",
round(injuries_per_incident, 1), "<br>", "Year: ", year)) %>%
add_markers(showlegend = F, ) %>%
add_lines(y = ~fitted(injuries_per_incident_)) %>%
layout(title = "Number of Injured",
xaxis = list(title = "Year"),
yaxis = list(title = "Injured"))
NA
Finally, we will look at the number of mass shooting incidents per year. In the last graph, it shows that the number of mass shootings has increased steadily over time.
num_per_year <- shootings %>%
filter(fatalities > 3) %>%
count(year)
num_per_year
shootings_per_year_model <- lm(n ~ year, data = num_per_year)
tidy(shootings_per_year_model)
glance(shootings_per_year_model)
NA
NA
shootings_per_year_model
Call:
lm(formula = n ~ year, data = num_per_year)
Coefficients:
(Intercept) year
-191.32228 0.09692
num_per_year %>%
plot_ly(x = ~year,
y = ~shootings_per_year_model,
hoverinfo = "text",
text = ~paste("Shootings Per Year: ",
(shootings_per_year_model), "<br>", "Year: ", year)) %>%
add_markers(showlegend = F, ) %>%
add_lines(y = ~fitted(shootings_per_year_model)) %>%
layout(title = "Number of Shootings by Year",
xaxis = list(title = "Year"),
yaxis = list(title = "Number of Shootings"))
NA