Data Sources:
Gun law strength: https://everytownresearch.org/rankings/methodology/
Deaths by Firearms: https://www.cdc.gov/nchs/pressroom/sosmap/firearm_mortality/firearm.htm
# Load required libraries
library(tidyverse)
library(sf)
library(ggplot2)
# Read the file into an sf object
us_states_hexgrid <- st_read("https://raw.githubusercontent.com/dianaplunkett/608/main/us_states_hexgrid.geojson")
## Reading layer `us_states_hexgrid' from data source
## `https://raw.githubusercontent.com/dianaplunkett/608/main/us_states_hexgrid.geojson'
## using driver `GeoJSON'
## Simple feature collection with 51 features and 7 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -137.9747 ymin: 26.39343 xmax: -69.90286 ymax: 55.3132
## Geodetic CRS: WGS 84
#Drop DC, as we have no other data for it.
us_states_hexgrid <- us_states_hexgrid[us_states_hexgrid$iso3166_2!='DC',]
# Plot the hexagons
ggplot() +
geom_sf(data = us_states_hexgrid) +
theme_void()
# Calculate centroids of the hexagons
centroids <- st_centroid(us_states_hexgrid)
# Convert centroids to a data frame
centroids_df <- st_drop_geometry(centroids) %>%
as.data.frame()
# Add state abbreviations to centroids_df
centroids_df$iso3166_2 <- us_states_hexgrid$iso3166_2
# Extract centroid coordinates
centroids_df$X <- st_coordinates(centroids)[, 1]
centroids_df$Y <- st_coordinates(centroids)[, 2]
# Plot the hexagons and add state abbreviations as labels
ggplot() +
geom_sf(data = us_states_hexgrid) +
geom_text(data = centroids_df, aes(x = X, y = Y, label = iso3166_2), size = 3, nudge_y = 0.1)+
theme_void()
dbf <- as.data.frame(read_csv ("https://raw.githubusercontent.com/dianaplunkett/608/main/data-table.csv"))
## Rows: 450 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): STATE, URL
## dbl (3): YEAR, RATE, DEATHS
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#just 2021 (most recent)
dbf <- filter(dbf, YEAR==2021)
us_states_hexgrid <- left_join(us_states_hexgrid, dbf, by = c("iso3166_2" = "STATE"))
# palette viridis is accessable
ggplot() +
geom_sf(data = us_states_hexgrid, aes(fill = RATE)) +
scale_fill_viridis_c(direction = -1) +
theme(legend.direction = "horizontal", legend.position = "bottom")+
theme_void()
gl <- as.data.frame(read_csv ("https://raw.githubusercontent.com/dianaplunkett/608/main/gun-laws.csv"))
## Rows: 50 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Series, Label
## dbl (2): Strength, Gun Deaths per 100,000 Residents
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
us_states_hexgrid <- left_join(us_states_hexgrid, gl, by = c("iso3166_2" = "Label"))
# palette viridis is accessible
ggplot() +
geom_sf(data = us_states_hexgrid, aes(fill = Strength)) +
scale_fill_viridis_c() +
theme(legend.direction = "horizontal", legend.position = "bottom")+
theme_void()
Cut Death Rate and Gun Law Strength in groups of 5
#create a 5 level death rate bin
# make numeric so can compare later
us_states_hexgrid$drBin <- as.numeric(cut(us_states_hexgrid$RATE, breaks=c(2.1, 8.46, 14.82, 21.18, 27.54, 33.9), labels = c(1, 2, 3, 4, 5)))
#create a 5 level gun law bin
# make numeric so can compare later
us_states_hexgrid$glBin <- as.numeric(cut(us_states_hexgrid$Strength, breaks=5, labels = c(1, 2, 3, 4, 5)))
#want strong laws= 1 and weak laws = 5, so need to reverse the bins. Could not so in cut, as when change to numeric, it still used the "order" of the factor to make numeric, not the value assigned to the factor
us_states_hexgrid <-us_states_hexgrid %>%
mutate (glBin = case_when(
glBin == 5 ~1,
glBin == 4 ~2,
glBin == 2 ~4,
glBin == 1 ~5,
TRUE ~glBin))
#what is the relationship btwn the bins?
us_states_hexgrid$binDiff <- ifelse(us_states_hexgrid$drBin == us_states_hexgrid$glBin, 1, ifelse(abs(us_states_hexgrid$drBin - us_states_hexgrid$glBin) <= 2, 2,
ifelse(abs(us_states_hexgrid$drBin - us_states_hexgrid$glBin) <= 3, 3,4)))
Map the difference in the Death Rates and Gun Laws.
ggplot() +
geom_sf(data = us_states_hexgrid,
aes(fill = as.factor(binDiff))) +
scale_fill_manual(values=c("#eeeeee", "#d3d3d3", "#b7b7b7", "#DC6601" )) +
geom_text(data = centroids_df, aes(x = X, y = Y, label = iso3166_2), size = 3, nudge_y = 0.1)+
theme_void()