Story -3 : Do stricter gun laws reduce firearm gun deaths?

The CDC publishes firearm mortality for each State per 100,000 persons https://www.cdc.gov/nchs/pressroom/sosmap/firearm_mortality/firearm.htm. Each State’ 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?”

For this assignment you will need to:

Access the firearm mortality data from the CDC using an available API (https://open.cdc.gov/apis.html)

Create a 5 point Likert scale categorizing gun control laws from most lax to strictest and assign each state to the most appropriate Likert bin.

Determine whether stricter gun control laws result in reduced gun violence deaths

Present your story using heat maps

# load libraries 

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(readr)

data <- read.csv('data-table.csv', stringsAsFactors = F)

data <- data[,1:4]
head(data)
##   YEAR STATE RATE DEATHS
## 1 2022    AL 25.5  1,278
## 2 2022    AK 22.4    164
## 3 2022    AZ 20.1  1,535
## 4 2022    AR 21.9    666
## 5 2022    CA  8.6  3,484
## 6 2022    CO 17.1  1,036

We will transform the dataset by incorporating additional geographic information, including state abbreviations, as well as the corresponding latitude and longitude coordinates. This transformation will allow us to enhance the dataset with precise location data, enabling the creation of more detailed visualizations and analyses, such as mapping the data to specific U.S. states and their respective positions on a geographic plot.

The first map you provided shows the level of strictness in gun laws across different states, with a color gradient ranging from yellow (least strict) to red (most strict, i.e., 5). However, this map alone does not provide information about firearm deaths or whether stricter gun laws are associated with a reduction in such deaths.

GUN_DATA <- data %>% mutate(gun_control_category = case_when(
  RATE <= quantile(RATE, 0.2) ~ 1,
  RATE <= quantile(RATE, 0.4) ~ 2,
  RATE <= quantile(RATE, 0.6) ~ 3,
  RATE <= quantile(RATE, 0.8) ~ 4,
  TRUE ~ 5 
))
GUN_DATA$DEATHS <-  as.numeric(gsub(",", "", GUN_DATA$DEATHS))



library(maps)
usa_map <- map_data('state')



# convert the data set to lower case 
#GUN_DATA$state <- tolower(GUN_DATA$STATE)
# Create a lookup table for state names and their abbreviations
state_abbreviations <- data.frame(
  state_annotaion = tolower(state.name),  # State names in lowercase to match map_data("state")
  STATE = state.abb  # State abbreviations
)

# Create the usa data set abbreviation 
usa_map <- merge(usa_map, state_abbreviations, by.x = "region", by.y = "state_annotaion")

#Merge the state map data with your dataset
data_final <- merge(usa_map, GUN_DATA, by.x = "STATE", by.y = "STATE")
map1<-ggplot(data_final, aes( x = long, y = lat, group=group)) +
  geom_polygon(aes(fill = gun_control_category), color = "black")


map2 <- map1 + scale_fill_gradient(name = "Level of Strictness in Gun Law ", low = "yellow", high =  "red", na.value = "grey50")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        rect = element_blank()) +  ggtitle("Level of Strictness in Gun Law")
map2

# Create the base map with gun law strictness
map1 <- ggplot(data_final, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = gun_control_category), color = "black")

# Add firearm death data as points, adjusting size based on death rate
map2 <- map1 + 
  geom_point(aes(x = long, y = lat, size = DEATHS), color = "blue", alpha = 0.7) +  # Overlay points for firearm deaths
  scale_fill_gradient(name = "Level of Strictness in Gun Law", low = "yellow", high = "red", na.value = "grey50") +
  scale_size_continuous(name = "Firearm Death Rate") +  # Legend for the size of the points
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        rect = element_blank()) + 
  ggtitle("Gun Law Strictness and Firearm Death Rates")

# Display the map
map2

map2 <- map1 + 
  geom_point(aes(x = long, y = lat, color = DEATHS), size = 3, alpha = 0.7) +  # Color-coded points for firearm deaths
  scale_fill_gradient(name = "Level of Strictness in Gun Law", low = "yellow", high = "red", na.value = "grey50") +
  scale_color_gradient(name = "Firearm Death Rate", low = "blue", high = "red") +  # Gradient color for death rate
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        rect = element_blank()) + 
  ggtitle("Gun Law Strictness and Firearm Death Rates")

map2

This map visualizes the relationship between gun law strictness and firearm death rates across U.S. states. The states are colored based on their level of gun law strictness, with a gradient from yellow (least strict) to red (most strict). Additionally, firearm death rates are overlaid as blue dots, with the size of the dots representing the magnitude of the death rates. Larger dots indicate higher firearm death rates, while smaller dots indicate lower death rates.

From the map, it appears that states with stricter gun laws (darker red areas) tend to have fewer large dots (lower firearm death rates), whereas states with more lenient gun laws (yellow or lighter areas) tend to have larger blue dots, indicating higher death rates. This suggests a possible correlation between stricter gun laws and lower firearm deaths.