## Warning: package 'plotly' was built under R version 4.3.3
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
# Set JSON data URL
url <- "https://data.cdc.gov/resource/489q-934x.json"
# Fetch JSON data by sending GET request
response_data <- GET(url)
# Parse JSON data to R list
list_data <- fromJSON(content(response_data, "text", encoding = "UTF-8"))
# Convert list to data frame
mortality_data <- as.data.frame(list_data)## 'data.frame': 880 obs. of 69 variables:
## $ year_and_quarter : chr "2021 Q1" "2021 Q1" "2021 Q1" "2021 Q1" ...
## $ time_period : chr "12 months ending with quarter" "12 months ending with quarter" "12 months ending with quarter" "12 months ending with quarter" ...
## $ cause_of_death : chr "All causes" "Alzheimer disease" "COVID-19" "Cancer" ...
## $ rate_type : chr "Age-adjusted" "Age-adjusted" "Age-adjusted" "Age-adjusted" ...
## $ unit : chr "Deaths per 100,000" "Deaths per 100,000" "Deaths per 100,000" "Deaths per 100,000" ...
## $ rate_overall : chr "866.3" "32.1" "120.7" "142" ...
## $ rate_sex_female : chr "716.3" "36.8" "94" "122.8" ...
## $ rate_sex_male : chr "1040.4" "24.8" "153.9" "167.7" ...
## $ rate_alaska : chr "779.2" "28.2" "44.4" "143" ...
## $ rate_alabama : chr "1123.4" "51.2" "160.2" "160.5" ...
## $ rate_arkansas : chr "1043.1" "44.1" "130.6" "160.7" ...
## $ rate_arizona : chr "853.2" "31.3" "142.1" "125.5" ...
## $ rate_california : chr "767.7" "41" "128.5" "128.5" ...
## $ rate_colorado : chr "760.9" "36" "83.6" "123.4" ...
## $ rate_connecticut : chr "784.3" "20.6" "138.9" "132.1" ...
## $ rate_district_of_columbia: chr "904.8" "11.1" "148.6" "136" ...
## $ rate_delaware : chr "866.2" "35.4" "106.8" "149.1" ...
## $ rate_florida : chr "757.3" "19.7" "80.6" "134.4" ...
## $ rate_georgia : chr "957.4" "45.9" "130.5" "145.8" ...
## $ rate_hawaii : chr "585.9" "22.4" "20.7" "124.1" ...
## $ rate_iowa : chr "848.3" "29.7" "119.9" "145.4" ...
## $ rate_idaho : chr "789.6" "37" "79.2" "135" ...
## $ rate_illinois : chr "861.5" "28.3" "121.3" "148.2" ...
## $ rate_indiana : chr "981.8" "32" "132.8" "160.6" ...
## $ rate_kansas : chr "901.2" "24.2" "121.8" "148.1" ...
## $ rate_kentucky : chr "1064.2" "31.7" "109.6" "177" ...
## $ rate_louisiana : chr "1080.3" "45.3" "146.7" "158.1" ...
## $ rate_massachusetts : chr "768" "17.8" "126" "132.8" ...
## $ rate_maryland : chr "847.9" "16" "111" "139.5" ...
## $ rate_maine : chr "785.3" "27.6" "36.1" "157.5" ...
## $ rate_michigan : chr "911.2" "36.3" "101.7" "155.5" ...
## $ rate_minnesota : chr "737.8" "32.9" "85.1" "136.4" ...
## $ rate_missouri : chr "956.2" "33.9" "113" "157.4" ...
## $ rate_mississippi : chr "1197" "58.3" "171.5" "172" ...
## $ rate_montana : chr "836.6" "22.6" "91.8" "136" ...
## $ rate_north_carolina : chr "893.7" "36.8" "99.3" "146" ...
## $ rate_north_dakota : chr "835.5" "37.6" "132.8" "136.7" ...
## $ rate_nebraska : chr "822.7" "32" "101.3" "143.3" ...
## $ rate_new_hampshire : chr "744.9" "25.8" "60.9" "143.8" ...
## $ rate_new_jersey : chr "853.1" "22.2" "172.6" "129.8" ...
## $ rate_new_mexico : chr "952.7" "25.9" "144.1" "130" ...
## $ rate_nevada : chr "895.2" "27.5" "133.4" "143.1" ...
## $ rate_new_york : chr "817.6" "14" "174.2" "125.7" ...
## $ rate_ohio : chr "983.2" "37.5" "122.2" "157.6" ...
## $ rate_oklahoma : chr "1063.5" "36.6" "157.8" "167.7" ...
## $ rate_oregon : chr "751.4" "36.4" "37.1" "146.9" ...
## $ rate_pennsylvania : chr "895.1" "23.5" "123" "150.8" ...
## $ rate_rhode_island : chr "828.9" "32" "138.4" "141.3" ...
## $ rate_south_carolina : chr "981.9" "40" "126.6" "150.5" ...
## $ rate_south_dakota : chr "882.7" "37.4" "145.8" "147" ...
## $ rate_tennessee : chr "1056.8" "42.8" "122.5" "161.9" ...
## $ rate_texas : chr "922" "44.9" "162.3" "137" ...
## $ rate_utah : chr "771.2" "41.1" "68.7" "116.2" ...
## $ rate_virginia : chr "824.8" "28.3" "92" "144.3" ...
## $ rate_vermont : chr "737.9" "34.3" "21.5" "151.9" ...
## $ rate_washington : chr "714.8" "42" "46.5" "139.8" ...
## $ rate_wisconsin : chr "825.8" "31.7" "86.1" "146.9" ...
## $ rate_west_virginia : chr "1096.9" "35.2" "94.2" "176.3" ...
## $ rate_wyoming : chr "854" "32.1" "84" "138" ...
## $ rate_age_1_4 : chr NA NA NA NA ...
## $ rate_age_5_14 : chr NA NA NA NA ...
## $ rate_age_15_24 : chr NA NA NA NA ...
## $ rate_age_25_34 : chr NA NA NA NA ...
## $ rate_age_35_44 : chr NA NA NA NA ...
## $ rate_age_45_54 : chr NA NA NA NA ...
## $ rate_age_55_64 : chr NA NA NA NA ...
## $ rate_65_74 : chr NA NA NA NA ...
## $ rate_age_75_84 : chr NA NA NA NA ...
## $ rate_age_85_plus : chr NA NA NA NA ...
## year_and_quarter time_period
## 1 2021 Q1 12 months ending with quarter
## 2 2021 Q1 12 months ending with quarter
## 3 2021 Q1 12 months ending with quarter
## 4 2021 Q1 12 months ending with quarter
## 5 2021 Q1 12 months ending with quarter
## 6 2021 Q1 12 months ending with quarter
## 7 2021 Q1 12 months ending with quarter
## 8 2021 Q1 12 months ending with quarter
## 9 2021 Q1 12 months ending with quarter
## 10 2021 Q1 12 months ending with quarter
## cause_of_death rate_type unit
## 1 All causes Age-adjusted Deaths per 100,000
## 2 Alzheimer disease Age-adjusted Deaths per 100,000
## 3 COVID-19 Age-adjusted Deaths per 100,000
## 4 Cancer Age-adjusted Deaths per 100,000
## 5 Chronic liver disease and cirrhosis Age-adjusted Deaths per 100,000
## 6 Chronic lower respiratory diseases Age-adjusted Deaths per 100,000
## 7 Diabetes Age-adjusted Deaths per 100,000
## 8 Drug overdose Age-adjusted Deaths per 100,000
## 9 Falls, ages 65 and over Age-adjusted Deaths per 100,000
## 10 Firearm-related injury Age-adjusted Deaths per 100,000
## rate_overall rate_sex_female rate_sex_male rate_alaska rate_alabama
## 1 866.3 716.3 1040.4 779.2 1123.4
## 2 32.1 36.8 24.8 28.2 51.2
## 3 120.7 94 153.9 44.4 160.2
## 4 142 122.8 167.7 143 160.5
## 5 13.9 9.8 18.3 23.6 17.2
## 6 33.8 30.9 37.5 28.4 50.4
## 7 25.4 19.9 31.9 25.3 24.9
## 8 30 18.2 41.9 25.1 25.1
## 9 69.6 60.4 82.4 58.2 30.9
## 10 14 4 24.4 23 24.6
## rate_arkansas rate_arizona rate_california rate_colorado rate_connecticut
## 1 1043.1 853.2 767.7 760.9 784.3
## 2 44.1 31.3 41 36 20.6
## 3 130.6 142.1 128.5 83.6 138.9
## 4 160.7 125.5 128.5 123.4 132.1
## 5 15.9 18.2 14.9 18.1 12.7
## 6 57.5 34.5 26.3 36.6 22.5
## 7 36.7 26.9 26.5 18.2 18.1
## 8 20.1 36.6 23.9 26.7 40.2
## 9 39.9 76.3 41.3 108.5 65.5
## 10 22.9 16.7 8.8 16.3 6.1
## rate_district_of_columbia rate_delaware rate_florida rate_georgia
## 1 904.8 866.2 757.3 957.4
## 2 11.1 35.4 19.7 45.9
## 3 148.6 106.8 80.6 130.5
## 4 136 149.1 134.4 145.8
## 5 9 10.6 13.4 12.5
## 6 18.9 32 31.2 39.5
## 7 25.4 23.5 23.6 24.8
## 8 59.2 49.4 35.9 19.7
## 9 53.7 50 73.3 53
## 10 21.9 14.7 13.8 18.3
## rate_hawaii rate_iowa rate_idaho rate_illinois rate_indiana rate_kansas
## 1 585.9 848.3 789.6 861.5 981.8 901.2
## 2 22.4 29.7 37 28.3 32 24.2
## 3 20.7 119.9 79.2 121.3 132.8 121.8
## 4 124.1 145.4 135 148.2 160.6 148.1
## 5 8.6 11.9 16 11.5 14.4 15.4
## 6 16.9 35.9 36.8 31.3 49.5 40.9
## 7 15.8 24.4 21.7 22.4 30.3 28.1
## 8 18.7 14.5 15.6 27.8 39.1 19.4
## 9 52.1 95.8 94.8 56.4 46.4 89.1
## 10 3.8 11 17.5 14.8 17.6 17.4
## rate_kentucky rate_louisiana rate_massachusetts rate_maryland rate_maine
## 1 1064.2 1080.3 768 847.9 785.3
## 2 31.7 45.3 17.8 16 27.6
## 3 109.6 146.7 126 111 36.1
## 4 177 158.1 132.8 139.5 157.5
## 5 17.5 11.1 11.5 9.6 14.2
## 6 52.7 38.9 25.2 25.3 36.8
## 7 29.1 34.1 17.5 24.5 24.7
## 8 53 48.5 35.2 45.7 41.4
## 9 45.3 44.5 82.9 74.2 117.4
## 10 21.1 28.4 3.7 14.4 10.2
## rate_michigan rate_minnesota rate_missouri rate_mississippi rate_montana
## 1 911.2 737.8 956.2 1197 836.6
## 2 36.3 32.9 33.9 58.3 22.6
## 3 101.7 85.1 113 171.5 91.8
## 4 155.5 136.4 157.4 172 136
## 5 14.5 14 12.9 16.3 21.6
## 6 38.5 28.3 43.4 56.7 38.8
## 7 26.3 20.8 23.6 43.6 24.1
## 8 30.2 21.3 33.2 24.5 16.3
## 9 78.8 126.2 71.8 68.4 109
## 10 14.9 9.1 24.7 30.7 20.9
## rate_north_carolina rate_north_dakota rate_nebraska rate_new_hampshire
## 1 893.7 835.5 822.7 744.9
## 2 36.8 37.6 32 25.8
## 3 99.3 132.8 101.3 60.9
## 4 146 136.7 143.3 143.8
## 5 13.1 17.9 14 12.5
## 6 35.4 32.1 40.2 30.5
## 7 27.9 22.5 25.9 20.3
## 8 33.8 15.2 11.2 28.7
## 9 81.8 84.3 68.6 100.7
## 10 16.1 15.2 11.1 9.3
## rate_new_jersey rate_new_mexico rate_nevada rate_new_york rate_ohio
## 1 853.1 952.7 895.2 817.6 983.2
## 2 22.2 25.9 27.5 14 37.5
## 3 172.6 144.1 133.4 174.2 122.2
## 4 129.8 130 143.1 125.7 157.6
## 5 9.7 36.8 16.6 8.3 13.1
## 6 22.8 38 40.6 22.9 40.4
## 7 21.5 29.7 24.7 21.2 29.1
## 8 32.9 43.1 28.5 27.1 49.8
## 9 32.1 86.7 65.5 43.4 80.6
## 10 5 23.8 18.1 5.5 15.9
## rate_oklahoma rate_oregon rate_pennsylvania rate_rhode_island
## 1 1063.5 751.4 895.1 828.9
## 2 36.6 36.4 23.5 32
## 3 157.8 37.1 123 138.4
## 4 167.7 146.9 150.8 141.3
## 5 18.9 16.5 10.4 13.7
## 6 56.3 32.4 30.2 24.1
## 7 33.4 23.6 24.2 22
## 8 20.7 20.6 44 39
## 9 111.4 106 80.9 102.1
## 10 20.9 14.2 14 5.4
## rate_south_carolina rate_south_dakota rate_tennessee rate_texas rate_utah
## 1 981.9 882.7 1056.8 922 771.2
## 2 40 37.4 42.8 44.9 41.1
## 3 126.6 145.8 122.5 162.3 68.7
## 4 150.5 147 161.9 137 116.2
## 5 16.3 29 16.8 16.4 10.7
## 6 41.9 33.6 47.6 34.3 30.2
## 7 28.5 29.1 30.5 27.7 27.8
## 8 36.3 10.6 50.5 15.4 21
## 9 70.4 123.5 75.6 57.9 94.8
## 10 21.8 15.5 22.3 14.5 13.9
## rate_virginia rate_vermont rate_washington rate_wisconsin rate_west_virginia
## 1 824.8 737.9 714.8 825.8 1096.9
## 2 28.3 34.3 42 31.7 35.2
## 3 92 21.5 46.5 86.1 94.2
## 4 144.3 151.9 139.8 146.9 176.3
## 5 11.8 12 15 12.8 17.4
## 6 29.9 33 27.5 32.4 56
## 7 24.9 17 23.3 21.8 42
## 8 29.2 37.4 23.7 28.3 91.3
## 9 68.4 140.3 100.5 171.7 109
## 10 13.2 12 10.7 12.3 17.4
## rate_wyoming rate_age_1_4 rate_age_5_14 rate_age_15_24 rate_age_25_34
## 1 854 <NA> <NA> <NA> <NA>
## 2 32.1 <NA> <NA> <NA> <NA>
## 3 84 <NA> <NA> <NA> <NA>
## 4 138 <NA> <NA> <NA> <NA>
## 5 29.1 <NA> <NA> <NA> <NA>
## 6 51.2 <NA> <NA> <NA> <NA>
## 7 19.8 <NA> <NA> <NA> <NA>
## 8 17.5 <NA> <NA> <NA> <NA>
## 9 79 <NA> <NA> <NA> <NA>
## 10 25 <NA> <NA> <NA> <NA>
## rate_age_35_44 rate_age_45_54 rate_age_55_64 rate_65_74 rate_age_75_84
## 1 <NA> <NA> <NA> <NA> <NA>
## 2 <NA> <NA> <NA> <NA> <NA>
## 3 <NA> <NA> <NA> <NA> <NA>
## 4 <NA> <NA> <NA> <NA> <NA>
## 5 <NA> <NA> <NA> <NA> <NA>
## 6 <NA> <NA> <NA> <NA> <NA>
## 7 <NA> <NA> <NA> <NA> <NA>
## 8 <NA> <NA> <NA> <NA> <NA>
## 9 <NA> <NA> <NA> <NA> <NA>
## 10 <NA> <NA> <NA> <NA> <NA>
## rate_age_85_plus
## 1 <NA>
## 2 <NA>
## 3 <NA>
## 4 <NA>
## 5 <NA>
## 6 <NA>
## 7 <NA>
## 8 <NA>
## 9 <NA>
## 10 <NA>
#Filtering the data for firearm-related injury and crude rate
df_gun <- mortality_data[mortality_data$cause_of_death == "Firearm-related injury" &
mortality_data$rate_type == "Crude" &
mortality_data$time_period == "12 months ending with quarter", ]
df_gun <- data.frame(df_gun, row.names = NULL)
#State abbreviations mapping
state_abbreviations <- c(AL = "alabama", AK = "alaska", AZ = "arizona", AR = "arkansas", CA = "california",
CO = "colorado", CT = "connecticut", DE = "delaware", FL = "florida", GA = "georgia",
HI = "hawaii", ID = "idaho", IL = "illinois", IN = "indiana", IA = "iowa",
KS = "kansas", KY = "kentucky", LA = "louisiana", ME = "maine", MD = "maryland",
MA = "massachusetts", MI = "michigan", MN = "minnesota", MS = "mississippi", MO = "missouri",
MT = "montana", NE = "nebraska", NV = "nevada", NH = "new_hampshire", NJ = "new_jersey",
NM = "new_mexico", NY = "new_york", NC = "north_carolina", ND = "north_dakota",
OH = "ohio", OK = "oklahoma", OR = "oregon", PA = "pennsylvania", RI = "rhode_island",
SC = "south_carolina", SD = "south_dakota", TN = "tennessee", TX = "texas", UT = "utah",
VT = "vermont", VA = "virginia", WA = "washington", WV = "west_virginia", WI = "wisconsin",
WY = "wyoming", DC = "district_of_columbia")
#Loop through each state abbreviation
for (abbrev in names(state_abbreviations)) {
pattern <- paste0("rate_", state_abbreviations[abbrev])
colnames(df_gun) <- gsub(pattern, abbrev, colnames(df_gun))
}
#Data type conversion: columns 6 to 69 are converted to double.
df_gun <- df_gun %>%
mutate(across(.cols = 6:69, .fns = as.double))
df_gun <- df_gun %>%
mutate(year = substr(year_and_quarter, 1, 4)) %>% # Extract year
group_by(year)
df_gun_2022 <- df_gun %>%
filter(year_and_quarter == "2022 Q4")
#Pivoting Long
df_gun_2022_long <- df_gun_2022 %>%
pivot_longer(
cols = c(AK:WY), # Specify the range of columns to pivot
names_to = "state", # New column name
values_to = "rate" # New column for values
)
final_df <- df_gun_2022_long %>%
select(year, state, rate)
#Adding gun law rank to final_df
final_df <- final_df %>%
mutate(
gun_laws = case_when(
state %in% c("AK", "AL", "AR", "AZ", "GA", "IA", "ID", "IN", "KS", "KY", "LA", "ME", "MO", "MS", "MT", "ND", "NH", "OH", "OK", "SC", "SD", "TN", "TX", "UT", "WV", "WY") ~ "1",
state == "WI" ~ "2",
state %in% c("FL", "MI", "MN", "NC", "NE", "NM", "NV", "VT") ~ "3",
state %in% c("CO", "DE", "OR", "PA", "RI", "VA", "WA") ~ "4",
state %in% c("CA", "CT", "DC", "HI", "IL", "MA", "MD", "NJ", "NY") ~ "5",
TRUE ~ NA_character_
)
)
final_df$gun_laws <- as.numeric(final_df$gun_laws)
final_df$year <- as.numeric(final_df$year)
#Displaying the final dataset
final_df## # A tibble: 51 × 4
## # Groups: year [1]
## year state rate gun_laws
## <dbl> <chr> <dbl> <dbl>
## 1 2022 AK 22.4 1
## 2 2022 AL 25.2 1
## 3 2022 AR 21.9 1
## 4 2022 AZ 20.9 1
## 5 2022 CA 8.9 5
## 6 2022 CO 17.7 4
## 7 2022 CT 6.9 5
## 8 2022 DC 22.9 5
## 9 2022 DE 12.2 4
## 10 2022 FL 14.5 3
## # ℹ 41 more rows
#Setting colors for each gun control laws strength category
category_colors <- c(
"1" = "#99c2a2", # most lax
"2" = "#66a366", # lax
"3" = "#e6e600", # moderate
"4" = "#ffcc80", # strict
"5" = "#ff6666" # most strict
)
#Creating Plotly heat map
heatmap <- plot_geo(final_df, locationmode = 'USA-states') %>%
add_trace(
z = final_df$gun_laws,
locations = final_df$state,
color = final_df$gun_laws,
colors = category_colors,
text = ~paste("State: ", state, "<br>Category: ", gun_laws, "<br>Death Rate: ", rate),
hoverinfo = "text"
)
#Customizing layout
heatmap <- heatmap %>%
layout(
title = "Gun Laws Strength by US State for year 2022 Q4",
geo = list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
)
#Adding colorbar legend
heatmap <- heatmap %>%
colorbar(
title = "Level of Strictness in Gun Control Laws.",
tickvals = 1:5,
ticktext = c("Most Lax", "Lax", "Moderate", "Strict", "Most Strict"),
ticks = "outside"
)
#Showing heatmap
heatmap#library(plotly)
#Calculating average rate for each gun laws strength category
avg_rate_by_category <- final_df %>%
group_by(gun_laws) %>%
summarize(Average_Rate = mean(rate))
#Setting colors for each category
category_colors <- c("blue", "green", "yellow", "orange", "red")
#Createing a custom color scale for legend
color_scale <- scale_fill_manual(values = category_colors)
#Creating bar chart
bar_chart <- plot_ly(avg_rate_by_category, x = ~gun_laws, y = ~Average_Rate, type = 'bar', marker = list(color = ~gun_laws, colors = category_colors)) %>%
layout(
title = "Firearm Mortality Rate by Gun Laws Strength Category",
xaxis = list(title = "Gun Laws Strength Category"),
yaxis = list(title = "Average Firearm Mortality Rate"),
showlegend = FALSE
)
#Showing bar chart
bar_chartThe visualizations reveal a pattern where states characterized by more lenient gun laws tend to exhibit higher rates of firearm-related deaths, whereas states enforcing the most stringent gun laws demonstrate the lowest death rates. In general, the bar chart suggests an association between stricter gun control laws and reduced mortality rates, with a minor anomaly: states with highly stringent gun laws show a slightly elevated death rate compared to those with moderately strengthened laws. This anomaly may be attributed to unaccounted factors that differ across states in this analysis. To sum up, it can be concluded that the implementation of stricter firearm control laws contributes to a decline in firearm mortality rates in U.S. states.