library(tidyverse)
library(jsonlite)
library(usmap)
library(viridis)
library(gridExtra)
library(httr)
library(ggplot2)
library(tidyr)
library(kableExtra)DATA_608_Story_03
Story - 3 : Do stricter gun laws reduce firearm gun deaths?
Introduction:
Gun control legislation has long been one of the most divisive issues confronting American policymakers. Debates over how firearms should be regulated consistently reveal deep ideological, cultural, and regional differences, making consensus difficult to achieve. This persistent divide has shaped decades of public policy discussions and continues to influence how states approach the balance between individual rights and public safety.
The CDC publishes firearm mortality for each State per 100,000 persons. Each State’s firearm control laws can be categorized as very strict to very lax. The purpose of this Story is to answer the question, ” Do stricter firearm control laws help reduce firearm mortality?”
The focus of this assignment will still be on picking the right visuals. The most recent year of focus will be 2024.
Data Collection and Analysis:
Load necessary libraries
1. Access the data via API
# 1. FETCH THE DATA
api_url <- URLencode("https://data.cdc.gov/resource/489q-934x.json?cause_of_death=Firearm-related injury&rate_type=Age-adjusted")
raw_cdc_data <- fromJSON(api_url)
# 2. FILTER FOR 2024 & PIVOT TO LONG FORMAT
cdc_2024_clean <- raw_cdc_data |>
filter(str_detect(year_and_quarter, "2024")) |>
# Sort to get the most recent 2024 quarter at the top, and slice just that top row
arrange(desc(year_and_quarter)) |>
slice(1) |>
# Grab only the columns that contain state rates
select(starts_with("rate_")) |>
# Remove the overall national/demographic rates so we are just left with states
select(-rate_overall, -rate_sex_female, -rate_sex_male) |>
# Pivot the wide data into two clean columns: state_name and mortality_rate
pivot_longer(
cols = everything(),
names_to = "state_name",
values_to = "mortality_rate"
) |>
mutate(
# Clean the state names (e.g., turns "rate_new_york" into "New York")
state_name = str_to_title(str_replace_all(str_remove(state_name, "rate_"), "_", " ")),
# Ensure the mortality rate is treated as a number
mortality_rate = as.numeric(mortality_rate)
)
cdc_2024_clean# A tibble: 52 × 2
state_name mortality_rate
<chr> <dbl>
1 Type NA
2 Alaska 24.4
3 Alabama 23.7
4 Arkansas 20.6
5 Arizona 16.9
6 California 7
7 Colorado 15.3
8 Connecticut 5.8
9 District Of Columbia 19.7
10 Delaware 11.8
# ℹ 42 more rows
2. Create a 5-point Likert Scale. The categorization is based on Giffords rankings.
# Categorize states from 1 (Most Lax) to 5 (Strictest)
gun_laws <- tibble(
state = state.abb,
law_score = case_when(
state %in% c("CA", "CT", "HI", "IL", "MA", "NJ", "NY", "CO", "MD", "DE", "OR", "RI", "WA") ~ 5, # Strictest
state %in% c("VA", "MN", "PA", "MI", "NV", "NM", "VT") ~ 4, # Strict
state %in% c("ME", "WI", "FL", "NE", "NC") ~ 3, # Moderate
state %in% c("IN") ~ 2, # Lax
TRUE ~ 1 # Most Lax (Default for remainder)
)
)
# Merge the CDC mortality data with our Likert scale
cdc_2024_clean_abb <- cdc_2024_clean |>
rename("state" = "state_name")
# 1. Create a reference lookup table
state_lookup <- tibble(
state_abb = state.abb,
state_full = state.name
)
# 2. Join to replace/add the full name
gun_laws <- gun_laws |>
left_join(state_lookup, by = c("state" = "state_abb"))
gun_laws_cleaned <- gun_laws |>
select(-state) |>
select(state_full, law_score) |>
rename(state = state_full)
analysis_data <- cdc_2024_clean_abb |>
left_join(gun_laws_cleaned, by = "state") |>
drop_na()
analysis_data# A tibble: 50 × 3
state mortality_rate law_score
<chr> <dbl> <dbl>
1 Alaska 24.4 1
2 Alabama 23.7 1
3 Arkansas 20.6 1
4 Arizona 16.9 1
5 California 7 5
6 Colorado 15.3 5
7 Connecticut 5.8 5
8 Delaware 11.8 5
9 Florida 12.7 3
10 Georgia 17.6 1
# ℹ 40 more rows
3. Create a Gun Law Strictness Heatmap and a Firearm Mortality Rate by State Heatmap
3.1 Firearm Mortality Heat Map
# Map 1: Firearm Mortality Heat Map
map_mortality <- plot_usmap(data = analysis_data, values = "mortality_rate", color = "black", labels = FALSE) +
scale_fill_viridis_c(
name = "Deaths per 100k",
option = "magma", # Accessible, high-contrast palette
direction = -1 # Reverses colors so darker means higher mortality
) +
theme(legend.position = "right") +
labs(title = "Firearm Mortality Rate by State")
map_mortalityComment:
Firearm mortality is regionally concentrated: the highest death rates cluster in the Southeast and parts of the West, while the Northeast shows the lowest rates. - Geographic clustering: contiguous states in the Southeast and some Western states show consistently higher mortality, suggesting regional drivers.
- Clear contrast with Northeast: New England and nearby states are among the lightest, indicating lower firearm mortalityFirearm mortality is regionally concentrated: the highest death rates cluster in the Southeast and parts of the West, while the Northeast shows the lowest rates.
- Geographic clustering: contiguous states in the Southeast and some Western states show consistently higher mortality, suggesting regional drivers. - Clear contrast with Northeast: New England and nearby states are among the lightest, indicating lower firearm mortality
3.2 Gun Law Strictness Heat Map
# Map 2: Gun Law Strictness Heat Map
# We treat the Likert scale as a discrete/categorical variable for the map
analysis_data$law_score_factor <- as.factor(analysis_data$law_score)
map_laws <- plot_usmap(data = analysis_data, values = "law_score_factor", color = "black", labels = FALSE) +
scale_fill_viridis_d(
name = "Strictness (1-5)",
option = "viridis", # Accessible palette
direction = 1
) +
theme(legend.position = "right") +
labs(title = "Gun Law Strictness (1=Most Lax, 5=Strictest)")
map_laws3. Correlation between gun control laws strictness and gun violence deaths.
correlation <- cor(analysis_data$mortality_rate, as.numeric(factor(analysis_data$law_score)))
correlation[1] -0.6546429
# 3. Let's Determine if stricter laws reduce deaths
# Group by the Likert scale and find the average mortality rate
summary_stats <- analysis_data |>
group_by(law_score) |>
summarize(avg_mortality = mean(mortality_rate), .groups = 'drop')
# Gun laws strictness and average deaths bar chart
bar_chart <- ggplot(summary_stats, aes(x = factor(law_score), y = avg_mortality, fill = factor(law_score))) +
geom_col() +
scale_fill_viridis_d(option = "plasma") +
labs(
title = "Firearm Mortality Rate by Gun Law Strength Category",
x = "Gun Law Strength (Likert 1–5)",
y = "Average Firearm Mortality Rate"
) +
theme_minimal() +
theme(legend.position = "none")
bar_chartComment:
The correlation between gun control laws strictness and gun violence deaths is -0.6546429.
At first, the correlation between gun control laws strictness and gun violence deaths is negative which could mean that stricter gun control laws are associated with less gun violence. However, computing the summary statistics to determine if stricter laws reduce deaths and plotting the bar chart, we realize that overall, the deaths tend to reduce as the gun laws are stricter. The most strict (5) the gun laws are, the least deaths are registered, and the least strict the gun laws are, the most deaths are registered. However, strict gun laws tend to have more deaths compared to moderate gun laws. Further analysis could help understand better this last observation.
Conclusion:
The visualizations indicate a clear pattern: states with more permissive gun laws tend to experience higher firearm‑related mortality, while those with the strictest regulations show the lowest death rates. Overall, the bar chart supports an association between stronger gun control measures and reduced firearm mortality. One minor irregularity appears in the data: states with the strict laws exhibit slightly higher mortality than those with moderately strict laws. This deviation is likely influenced by additional state‑level factors not accounted for in this analysis. Taken together, the findings suggest that in 2024, stricter firearm regulations are generally associated with lower firearm mortality across U.S. states.
Comment:
The Gun Law Strictness map shows a clear regional pattern: stricter gun laws cluster in the Northeast and on the West Coast, while permissive laws dominate much of the southern and the central U.S. In other words, there is a regional clustering where states with the highest strictness scores are concentrated in specific regions rather than scattered randomly.