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?”

Access the firearm mortality data

using an available API (https://open.cdc.gov/apis.html) and RScocrata packages

df <- read.socrata("https://data.cdc.gov/resource/489q-934x.json?$$app_token=tdWMkm9ddsLc6QKHvBP6aCiOA")
head(df)
##   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
##                        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
##   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
##   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
##   rate_district_of_columbia rate_delaware rate_florida rate_georgia rate_hawaii
## 1                     904.8         866.2        757.3        957.4       585.9
## 2                      11.1          35.4         19.7         45.9        22.4
## 3                     148.6         106.8         80.6        130.5        20.7
## 4                       136         149.1        134.4        145.8       124.1
## 5                         9          10.6         13.4         12.5         8.6
## 6                      18.9            32         31.2         39.5        16.9
##   rate_iowa rate_idaho rate_illinois rate_indiana rate_kansas rate_kentucky
## 1     848.3      789.6         861.5        981.8       901.2        1064.2
## 2      29.7         37          28.3           32        24.2          31.7
## 3     119.9       79.2         121.3        132.8       121.8         109.6
## 4     145.4        135         148.2        160.6       148.1           177
## 5      11.9         16          11.5         14.4        15.4          17.5
## 6      35.9       36.8          31.3         49.5        40.9          52.7
##   rate_louisiana rate_massachusetts rate_maryland rate_maine rate_michigan
## 1         1080.3                768         847.9      785.3         911.2
## 2           45.3               17.8            16       27.6          36.3
## 3          146.7                126           111       36.1         101.7
## 4          158.1              132.8         139.5      157.5         155.5
## 5           11.1               11.5           9.6       14.2          14.5
## 6           38.9               25.2          25.3       36.8          38.5
##   rate_minnesota rate_missouri rate_mississippi rate_montana
## 1          737.8         956.2             1197        836.6
## 2           32.9          33.9             58.3         22.6
## 3           85.1           113            171.5         91.8
## 4          136.4         157.4              172          136
## 5             14          12.9             16.3         21.6
## 6           28.3          43.4             56.7         38.8
##   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
##   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
##   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
##   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
##   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
##   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>
##   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>
##   rate_age_85_plus
## 1             <NA>
## 2             <NA>
## 3             <NA>
## 4             <NA>
## 5             <NA>
## 6             <NA>
# Select data for fire-arm related injury
df2 <- df %>%
  filter(cause_of_death == "Firearm-related injury" & 
         rate_type == "Crude" & 
         time_period == "12 months ending with quarter") %>%
  data.frame(row.names = NULL)
df2
##    year_and_quarter                   time_period         cause_of_death
## 1           2021 Q1 12 months ending with quarter Firearm-related injury
## 2           2021 Q2 12 months ending with quarter Firearm-related injury
## 3           2021 Q3 12 months ending with quarter Firearm-related injury
## 4           2021 Q4 12 months ending with quarter Firearm-related injury
## 5           2022 Q1 12 months ending with quarter Firearm-related injury
## 6           2022 Q2 12 months ending with quarter Firearm-related injury
## 7           2022 Q3 12 months ending with quarter Firearm-related injury
## 8           2022 Q4 12 months ending with quarter Firearm-related injury
## 9           2023 Q1 12 months ending with quarter Firearm-related injury
## 10          2023 Q2 12 months ending with quarter Firearm-related injury
##    rate_type               unit rate_overall rate_sex_female rate_sex_male
## 1      Crude Deaths per 100,000         14.1             3.9          24.5
## 2      Crude Deaths per 100,000         14.4               4            25
## 3      Crude Deaths per 100,000         14.6             4.1          25.3
## 4      Crude Deaths per 100,000         14.7             4.2          25.5
## 5      Crude Deaths per 100,000         14.8             4.1          25.6
## 6      Crude Deaths per 100,000         14.8             4.2          25.6
## 7      Crude Deaths per 100,000         14.7             4.2          25.3
## 8      Crude Deaths per 100,000         14.5             4.1            25
## 9      Crude Deaths per 100,000         14.4             4.1          24.8
## 10     Crude Deaths per 100,000         <NA>            <NA>          <NA>
##    rate_alaska rate_alabama rate_arkansas rate_arizona rate_california
## 1         23.1         24.2          22.6         17.1               9
## 2         25.1         24.8          22.9         18.3             9.3
## 3         24.4         25.4          22.7         18.4             9.3
## 4         24.8         26.1          23.1         18.8             9.1
## 5         25.8         25.4          22.1         19.3             9.1
## 6         23.7         25.2          21.7         20.5               9
## 7         23.4         25.6          22.4         20.9             9.1
## 8         22.4         25.2          21.9         20.9             8.9
## 9         21.5         26.1          22.9         20.7             8.9
## 10        <NA>         <NA>          <NA>         <NA>            <NA>
##    rate_colorado rate_connecticut rate_district_of_columbia rate_delaware
## 1           16.7              6.3                      23.7          13.9
## 2           17.3                7                      24.7          15.4
## 3           16.7              7.3                      26.5          15.9
## 4           18.3              6.9                      27.6          15.7
## 5           17.6              7.3                      26.6          15.8
## 6           17.3              7.1                      27.9          13.9
## 7           17.8              6.8                      26.1          12.6
## 8           17.7              6.9                      22.9          12.2
## 9           17.8              6.6                      25.3          12.6
## 10          <NA>             <NA>                      <NA>          <NA>
##    rate_florida rate_georgia rate_hawaii rate_iowa rate_idaho rate_illinois
## 1          14.1         18.3           4      10.9       17.5          14.6
## 2          14.5         19.3         4.7      10.9       16.5          14.4
## 3          14.3           20         5.1      10.9       17.1            15
## 4          14.4         20.4         4.9      11.4       16.3          15.7
## 5          14.3         20.7           5      11.3       16.2          15.5
## 6          14.6         20.6         4.6      11.1       17.6          15.4
## 7          14.5         19.8         4.2      10.9       16.8          14.7
## 8          14.5         19.8         4.6      11.5       17.4          14.3
## 9          14.8         19.5         4.4      12.3       17.7          14.3
## 10         <NA>         <NA>        <NA>      <NA>       <NA>          <NA>
##    rate_indiana rate_kansas rate_kentucky rate_louisiana rate_massachusetts
## 1          17.6        17.4          20.9           27.4                3.8
## 2          17.9        16.9          21.8           28.9                3.5
## 3          18.2        17.7          21.9             29                3.3
## 4          18.4        17.1            21           28.4                3.5
## 5            18        17.9          20.5           28.5                3.7
## 6          18.5        18.2          19.8           28.9                3.8
## 7          17.9        17.5          18.7           27.8                3.9
## 8          17.7        16.8          18.6           27.6                3.8
## 9          18.2          16          18.2           27.4                3.8
## 10         <NA>        <NA>          <NA>           <NA>               <NA>
##    rate_maryland rate_maine rate_michigan rate_minnesota rate_missouri
## 1           14.1       11.5          14.9            9.3            24
## 2           14.3       12.5          15.5            9.6          23.9
## 3           14.9         13            16             10          23.1
## 4           14.8         13          15.4             10          22.9
## 5           14.6       12.6          15.6           10.1          23.3
## 6           14.1       12.5          15.5           10.5          23.7
## 7           13.5       11.7          15.1           10.2          23.8
## 8           13.2       12.9            15            9.8          24.1
## 9           12.8       12.7          14.5            9.6          23.5
## 10          <NA>       <NA>          <NA>           <NA>          <NA>
##    rate_mississippi rate_montana rate_north_carolina rate_north_dakota
## 1              29.5         21.8                16.2              14.5
## 2              30.9         22.1                17.1              14.8
## 3                33         23.3                17.2                16
## 4              32.6         25.4                17.4              16.5
## 5              32.3         25.5                17.5              16.4
## 6              31.2           24                17.2              15.8
## 7              28.4         24.2                17.3              15.7
## 8              28.8         24.4                  17                16
## 9              28.3         23.4                16.8                14
## 10             <NA>         <NA>                <NA>              <NA>
##    rate_nebraska rate_new_hampshire rate_new_jersey rate_new_mexico rate_nevada
## 1             11                9.9             4.9            23.6        18.6
## 2           10.7               10.2             4.9            25.5        19.3
## 3           10.1                  9               5            26.1        20.5
## 4           10.2                8.9             5.1            27.3        20.1
## 5           10.4                8.7             5.5            27.5        19.5
## 6           11.4                9.3             5.4            27.1        19.7
## 7           12.6               10.3             5.3            28.1        19.1
## 8           12.4               11.2             5.1              27        19.4
## 9           11.4               11.6               5            26.9        19.4
## 10          <NA>               <NA>            <NA>            <NA>        <NA>
##    rate_new_york rate_ohio rate_oklahoma rate_oregon rate_pennsylvania
## 1            5.6      15.6          20.7        15.2                14
## 2            5.5        16          20.5        15.3              14.3
## 3            5.3      15.9          21.2        15.5              14.4
## 4            5.4      16.2            21        15.8              14.7
## 5            5.6      16.3          20.4        15.5              15.2
## 6            5.5      16.2          20.1        15.6              15.3
## 7            5.5      15.9          19.8          16              15.6
## 8            5.3      15.6          19.8        15.4                15
## 9              5      15.6          19.4        15.3              14.6
## 10          <NA>      <NA>          <NA>        <NA>              <NA>
##    rate_rhode_island rate_south_carolina rate_south_dakota rate_tennessee
## 1                5.5                21.3              15.2           22.2
## 2                6.1                21.8              15.4           22.6
## 3                6.4                21.8              14.1           22.7
## 4                5.8                21.9              14.3           22.5
## 5                5.5                21.5              14.7           22.3
## 6                4.6                21.6              14.8           22.5
## 7                3.7                21.2              16.7           21.8
## 8                3.4                20.9              15.5             21
## 9                3.7                21.8              14.6           21.4
## 10              <NA>                <NA>              <NA>           <NA>
##    rate_texas rate_utah rate_virginia rate_vermont rate_washington
## 1        14.5      13.4          13.4         12.5              11
## 2        15.1      13.6          13.9           12            10.7
## 3        15.3      14.3          13.7         12.7            11.2
## 4        15.6      13.5          14.4         12.9            11.6
## 5        15.9      12.9            15           13            12.3
## 6          16      12.5          15.2         14.1            12.8
## 7          16      12.4          15.3         13.5              13
## 8        15.4      13.2          15.1           13            13.1
## 9          15      14.1          14.9         13.1            13.2
## 10       <NA>      <NA>          <NA>         <NA>            <NA>
##    rate_wisconsin rate_west_virginia rate_wyoming rate_age_1_4 rate_age_5_14
## 1            12.3               17.8         25.2          0.8           1.7
## 2            12.8               18.2         24.7          0.8           1.7
## 3            13.1               18.4         23.9          0.8           1.7
## 4            13.5               17.9         26.8          0.9           1.6
## 5            14.5               17.4         27.1            1           1.6
## 6            14.5               17.8         27.1            1           1.6
## 7            14.6                 18         23.8          0.9           1.6
## 8            14.1               17.5         21.3          0.9           1.5
## 9            13.8               18.6         20.4          0.8           1.6
## 10           <NA>               <NA>         <NA>         <NA>          <NA>
##    rate_age_15_24 rate_age_25_34 rate_age_35_44 rate_age_45_54 rate_age_55_64
## 1              23           23.6           17.3           13.6           11.7
## 2            23.7           24.7           17.7           13.8           11.7
## 3            23.7             25             18             14           11.7
## 4            23.5           24.8           18.1           14.5           12.1
## 5            23.3           24.5           18.4           14.7           12.3
## 6            22.4           24.2           18.4           14.9           12.9
## 7            21.9           23.6           18.3           14.9           13.1
## 8            21.1           22.9           18.1           14.8           13.4
## 9              21           22.5             18           14.7           13.5
## 10           <NA>           <NA>           <NA>           <NA>           <NA>
##    rate_65_74 rate_age_75_84 rate_age_85_plus
## 1        10.9           15.1               16
## 2          11           15.7             17.3
## 3        11.3           16.1             17.8
## 4        11.7           16.2             18.3
## 5        11.7           16.3             19.2
## 6        12.1           16.6             18.1
## 7        12.1           16.3             18.6
## 8        11.9           16.5             18.5
## 9          12           16.3             17.7
## 10       <NA>           <NA>             <NA>

Data Transformation

# Remove columns with null data
columns_empty_data <- c(
  "rate_type", "unit", "rate_overall", "rate_sex_female", "rate_sex_male",
  "rate_age_1_4", "rate_age_5_14", "rate_age_15_24", "rate_age_25_34",
  "rate_age_35_44", "rate_age_45_54", "rate_age_55_64", "rate_65_74",
  "rate_age_75_84", "rate_age_85_plus"
)

# Subset the data frame to exclude specific columns
df3 <- df2[, !names(df2) %in% columns_empty_data]


# Rename States' columns with corresponding Acronyms
colnames(df3)[colnames(df3) == "rate_alaska"] <- "AK"
colnames(df3)[colnames(df3) == "rate_alabama"] <- "AL"
colnames(df3)[colnames(df3) == "rate_arkansas"] <- "AR"
colnames(df3)[colnames(df3) == "rate_arizona"] <- "AZ"
colnames(df3)[colnames(df3) == "rate_california"] <- "CA"
colnames(df3)[colnames(df3) == "rate_district_of_columbia"] <- "DC"
colnames(df3)[colnames(df3) == "rate_colorado"] <- "CO"
colnames(df3)[colnames(df3) == "rate_connecticut"] <- "CT"
colnames(df3)[colnames(df3) == "rate_delaware"] <- "DE"
colnames(df3)[colnames(df3) == "rate_florida"] <- "FL"
colnames(df3)[colnames(df3) == "rate_georgia"] <- "GA"
colnames(df3)[colnames(df3) == "rate_hawaii"] <- "HI"
colnames(df3)[colnames(df3) == "rate_iowa"] <- "IA"
colnames(df3)[colnames(df3) == "rate_idaho"] <- "ID"
colnames(df3)[colnames(df3) == "rate_illinois"] <- "IL"
colnames(df3)[colnames(df3) == "rate_indiana"] <- "IN"
colnames(df3)[colnames(df3) == "rate_kansas"] <- "KS"
colnames(df3)[colnames(df3) == "rate_kentucky"] <- "KY"
colnames(df3)[colnames(df3) == "rate_louisiana"] <- "LA"
colnames(df3)[colnames(df3) == "rate_massachusetts"] <- "MA"
colnames(df3)[colnames(df3) == "rate_maryland"] <- "MD"
colnames(df3)[colnames(df3) == "rate_maine"] <- "ME"
colnames(df3)[colnames(df3) == "rate_michigan"] <- "MI"
colnames(df3)[colnames(df3) == "rate_minnesota"] <- "MN"
colnames(df3)[colnames(df3) == "rate_missouri"] <- "MO"
colnames(df3)[colnames(df3) == "rate_mississippi"] <- "MS"
colnames(df3)[colnames(df3) == "rate_montana"] <- "MT"
colnames(df3)[colnames(df3) == "rate_north_carolina"] <- "NC"
colnames(df3)[colnames(df3) == "rate_north_dakota"] <- "ND"
colnames(df3)[colnames(df3) == "rate_nebraska"] <- "NE"
colnames(df3)[colnames(df3) == "rate_new_hampshire"] <- "NH"
colnames(df3)[colnames(df3) == "rate_new_jersey"] <- "NJ"
colnames(df3)[colnames(df3) == "rate_new_mexico"] <- "NM"
colnames(df3)[colnames(df3) == "rate_nevada"] <- "NV"
colnames(df3)[colnames(df3) == "rate_new_york"] <- "NY"
colnames(df3)[colnames(df3) == "rate_ohio"] <- "OH"
colnames(df3)[colnames(df3) == "rate_oklahoma"] <- "OK"
colnames(df3)[colnames(df3) == "rate_oregon"] <- "OR"
colnames(df3)[colnames(df3) == "rate_pennsylvania"] <- "PA"
colnames(df3)[colnames(df3) == "rate_rhode_island"] <- "RI"
colnames(df3)[colnames(df3) == "rate_south_carolina"] <- "SC"
colnames(df3)[colnames(df3) == "rate_south_dakota"] <- "SD"
colnames(df3)[colnames(df3) == "rate_tennessee"] <- "TN"
colnames(df3)[colnames(df3) == "rate_texas"] <- "TX"
colnames(df3)[colnames(df3) == "rate_utah"] <- "UT"
colnames(df3)[colnames(df3) == "rate_virginia"] <- "VA"
colnames(df3)[colnames(df3) == "rate_vermont"] <- "VT"
colnames(df3)[colnames(df3) == "rate_washington"] <- "WA"
colnames(df3)[colnames(df3) == "rate_wisconsin"] <- "WI"
colnames(df3)[colnames(df3) == "rate_west_virginia"] <- "WV"
colnames(df3)[colnames(df3) == "rate_wyoming"] <- "WY"
print(df3)
##    year_and_quarter                   time_period         cause_of_death   AK
## 1           2021 Q1 12 months ending with quarter Firearm-related injury 23.1
## 2           2021 Q2 12 months ending with quarter Firearm-related injury 25.1
## 3           2021 Q3 12 months ending with quarter Firearm-related injury 24.4
## 4           2021 Q4 12 months ending with quarter Firearm-related injury 24.8
## 5           2022 Q1 12 months ending with quarter Firearm-related injury 25.8
## 6           2022 Q2 12 months ending with quarter Firearm-related injury 23.7
## 7           2022 Q3 12 months ending with quarter Firearm-related injury 23.4
## 8           2022 Q4 12 months ending with quarter Firearm-related injury 22.4
## 9           2023 Q1 12 months ending with quarter Firearm-related injury 21.5
## 10          2023 Q2 12 months ending with quarter Firearm-related injury <NA>
##      AL   AR   AZ   CA   CO   CT   DC   DE   FL   GA   HI   IA   ID   IL   IN
## 1  24.2 22.6 17.1    9 16.7  6.3 23.7 13.9 14.1 18.3    4 10.9 17.5 14.6 17.6
## 2  24.8 22.9 18.3  9.3 17.3    7 24.7 15.4 14.5 19.3  4.7 10.9 16.5 14.4 17.9
## 3  25.4 22.7 18.4  9.3 16.7  7.3 26.5 15.9 14.3   20  5.1 10.9 17.1   15 18.2
## 4  26.1 23.1 18.8  9.1 18.3  6.9 27.6 15.7 14.4 20.4  4.9 11.4 16.3 15.7 18.4
## 5  25.4 22.1 19.3  9.1 17.6  7.3 26.6 15.8 14.3 20.7    5 11.3 16.2 15.5   18
## 6  25.2 21.7 20.5    9 17.3  7.1 27.9 13.9 14.6 20.6  4.6 11.1 17.6 15.4 18.5
## 7  25.6 22.4 20.9  9.1 17.8  6.8 26.1 12.6 14.5 19.8  4.2 10.9 16.8 14.7 17.9
## 8  25.2 21.9 20.9  8.9 17.7  6.9 22.9 12.2 14.5 19.8  4.6 11.5 17.4 14.3 17.7
## 9  26.1 22.9 20.7  8.9 17.8  6.6 25.3 12.6 14.8 19.5  4.4 12.3 17.7 14.3 18.2
## 10 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
##      KS   KY   LA   MA   MD   ME   MI   MN   MO   MS   MT   NC   ND   NE   NH
## 1  17.4 20.9 27.4  3.8 14.1 11.5 14.9  9.3   24 29.5 21.8 16.2 14.5   11  9.9
## 2  16.9 21.8 28.9  3.5 14.3 12.5 15.5  9.6 23.9 30.9 22.1 17.1 14.8 10.7 10.2
## 3  17.7 21.9   29  3.3 14.9   13   16   10 23.1   33 23.3 17.2   16 10.1    9
## 4  17.1   21 28.4  3.5 14.8   13 15.4   10 22.9 32.6 25.4 17.4 16.5 10.2  8.9
## 5  17.9 20.5 28.5  3.7 14.6 12.6 15.6 10.1 23.3 32.3 25.5 17.5 16.4 10.4  8.7
## 6  18.2 19.8 28.9  3.8 14.1 12.5 15.5 10.5 23.7 31.2   24 17.2 15.8 11.4  9.3
## 7  17.5 18.7 27.8  3.9 13.5 11.7 15.1 10.2 23.8 28.4 24.2 17.3 15.7 12.6 10.3
## 8  16.8 18.6 27.6  3.8 13.2 12.9   15  9.8 24.1 28.8 24.4   17   16 12.4 11.2
## 9    16 18.2 27.4  3.8 12.8 12.7 14.5  9.6 23.5 28.3 23.4 16.8   14 11.4 11.6
## 10 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
##      NJ   NM   NV   NY   OH   OK   OR   PA   RI   SC   SD   TN   TX   UT   VA
## 1   4.9 23.6 18.6  5.6 15.6 20.7 15.2   14  5.5 21.3 15.2 22.2 14.5 13.4 13.4
## 2   4.9 25.5 19.3  5.5   16 20.5 15.3 14.3  6.1 21.8 15.4 22.6 15.1 13.6 13.9
## 3     5 26.1 20.5  5.3 15.9 21.2 15.5 14.4  6.4 21.8 14.1 22.7 15.3 14.3 13.7
## 4   5.1 27.3 20.1  5.4 16.2   21 15.8 14.7  5.8 21.9 14.3 22.5 15.6 13.5 14.4
## 5   5.5 27.5 19.5  5.6 16.3 20.4 15.5 15.2  5.5 21.5 14.7 22.3 15.9 12.9   15
## 6   5.4 27.1 19.7  5.5 16.2 20.1 15.6 15.3  4.6 21.6 14.8 22.5   16 12.5 15.2
## 7   5.3 28.1 19.1  5.5 15.9 19.8   16 15.6  3.7 21.2 16.7 21.8   16 12.4 15.3
## 8   5.1   27 19.4  5.3 15.6 19.8 15.4   15  3.4 20.9 15.5   21 15.4 13.2 15.1
## 9     5 26.9 19.4    5 15.6 19.4 15.3 14.6  3.7 21.8 14.6 21.4   15 14.1 14.9
## 10 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
##      VT   WA   WI   WV   WY
## 1  12.5   11 12.3 17.8 25.2
## 2    12 10.7 12.8 18.2 24.7
## 3  12.7 11.2 13.1 18.4 23.9
## 4  12.9 11.6 13.5 17.9 26.8
## 5    13 12.3 14.5 17.4 27.1
## 6  14.1 12.8 14.5 17.8 27.1
## 7  13.5   13 14.6   18 23.8
## 8    13 13.1 14.1 17.5 21.3
## 9  13.1 13.2 13.8 18.6 20.4
## 10 <NA> <NA> <NA> <NA> <NA>
# Data type conversion: Columns 2 to 5 are kept as character, columns 6 to 54 are converted to double.
df4 <- df3 %>%
  mutate(across(.cols = 4:54, .fns = as.double))

# Extract year information and group it
df5 <- df4 %>%
  mutate(Year = substr(year_and_quarter, 1, 4)) %>%  # Extract year
  group_by(Year)  # Group by year

df5_grouped_by_year <- df5%>%
  filter(grepl("2022", year_and_quarter))

# Piving Long
df5_grouped_by_year_long <- df5_grouped_by_year %>%
                          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 <- df5_grouped_by_year_long %>%
          select(Year, State, Rate)

head(final_df)
## # A tibble: 6 × 3
## # Groups:   Year [1]
##   Year  State  Rate
##   <chr> <chr> <dbl>
## 1 2022  AK     25.8
## 2 2022  AL     25.4
## 3 2022  AR     22.1
## 4 2022  AZ     19.3
## 5 2022  CA      9.1
## 6 2022  CO     17.6

Data transformation contributes to stricter gun laws ranked by States per the gun law strength data for year 2022. This data are collected from https://giffords.org/lawcenter/resources/scorecard2022/ . The rank for the DC is not available in the site.

# Remove rows where the column "State" contains "DC":
final_df <- subset(final_df, State != "DC")

# gun law Strength (Ranked)
final_df <- final_df %>%
  mutate(
    gun_laws_strength_rank = case_when(
      State == "AK" ~ "41",
      State == "AL" ~ "38",
      State == "AR" ~ "50",
      State == "AZ" ~ "42",
      State == "CA" ~ "1",
      State == "CO" ~ "14",
      State == "CT" ~ "3",
      State == "DE" ~ "13",
      State == "FL" ~ "23",
      State == "GA" ~ "34",
      State == "HI" ~ "5",
      State == "IA" ~ "32",
      State == "ID" ~ "48",
      State == "IL" ~ "8",
      State == "IN" ~ "29",
      State == "KS" ~ "45",
      State == "KY" ~ "43",
      State == "LA" ~ "32",
      State == "MA" ~ "6",
      State == "MD" ~ "7",
      State == "ME" ~ "27",
      State == "MI" ~ "16",
      State == "MN" ~ "18",
      State == "MO" ~ "47",
      State == "MS" ~ "45",
      State == "MT" ~ "40",
      State == "NC" ~ "21",
      State == "ND" ~ "37",
      State == "NE" ~ "20",
      State == "NH" ~ "26",
      State == "NJ" ~ "2",
      State == "NM" ~ "18",
      State == "NV" ~ "17",
      State == "NY" ~ "4",
      State == "OH" ~ "25",
      State == "OK" ~ "34",
      State == "OR" ~ "11",
      State == "PA" ~ "15",
      State == "RI" ~ "9",
      State == "SC" ~ "28",
      State == "SD" ~ "44",
      State == "TN" ~ "38",
      State == "TX" ~ "30",
      State == "UT" ~ "30",
      State == "VA" ~ "12",
      State == "VT" ~ "22",
      State == "WA" ~ "10",
      State == "WI" ~ "24",
      State == "WV" ~ "36",
      State == "WY" ~ "49",
      TRUE ~ NA_character_
    )
  )

head(final_df)
## # A tibble: 6 × 4
## # Groups:   Year [1]
##   Year  State  Rate gun_laws_strength_rank
##   <chr> <chr> <dbl> <chr>                 
## 1 2022  AK     25.8 41                    
## 2 2022  AL     25.4 38                    
## 3 2022  AR     22.1 50                    
## 4 2022  AZ     19.3 42                    
## 5 2022  CA      9.1 1                     
## 6 2022  CO     17.6 14

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

# Convert gun_laws_strength_rank column to numeric
final_df$gun_laws_strength_rank <- as.numeric(final_df$gun_laws_strength_rank)

# Set breaks for Likert scale 
breaks <- c(0, 10, 20, 30, 40, 50)

# Create Likert scale labels
labels <- c("most strict", "strict", "moderate", "lax", "most lax")

# Cut data into Likert scale categories
final_df$gun_laws_strength_categories <- cut(final_df$gun_laws_strength_rank , breaks = breaks, labels = labels, include.lowest = TRUE)

# Display the categorized data
print(final_df)
## # A tibble: 200 × 5
## # Groups:   Year [1]
##    Year  State  Rate gun_laws_strength_rank gun_laws_strength_categories
##    <chr> <chr> <dbl>                  <dbl> <fct>                       
##  1 2022  AK     25.8                     41 most lax                    
##  2 2022  AL     25.4                     38 lax                         
##  3 2022  AR     22.1                     50 most lax                    
##  4 2022  AZ     19.3                     42 most lax                    
##  5 2022  CA      9.1                      1 most strict                 
##  6 2022  CO     17.6                     14 strict                      
##  7 2022  CT      7.3                      3 most strict                 
##  8 2022  DE     15.8                     13 strict                      
##  9 2022  FL     14.3                     23 moderate                    
## 10 2022  GA     20.7                     34 lax                         
## # ℹ 190 more rows
# Define Likert scale categories
likert_categories <- c(
  "Most Strict" = 1 , "Strict" = 2, "Moderate" = 3, "Lax" = 4 ,  "Most Lax" = 5
)

# Create a new column with numeric values
final_df$gun_laws_strength_value <- likert_categories[final_df$gun_laws_strength_categories]
head(final_df)
## # A tibble: 6 × 6
## # Groups:   Year [1]
##   Year  State  Rate gun_laws_strength_rank gun_laws_strength_categories
##   <chr> <chr> <dbl>                  <dbl> <fct>                       
## 1 2022  AK     25.8                     41 most lax                    
## 2 2022  AL     25.4                     38 lax                         
## 3 2022  AR     22.1                     50 most lax                    
## 4 2022  AZ     19.3                     42 most lax                    
## 5 2022  CA      9.1                      1 most strict                 
## 6 2022  CO     17.6                     14 strict                      
## # ℹ 1 more variable: gun_laws_strength_value <dbl>

Exploratory Data Analysis

to determine whether stricter gun control laws result in reduced gun violence deaths

#Regression analysis with scatterplot 
scatterplot <- ggplot(final_df, aes(x = gun_laws_strength_rank, y = Rate, label = State, color = gun_laws_strength_categories)) +
  geom_point() +
  geom_text(hjust = 0, nudge_x = 0.1, nudge_y = 0.05) +
  geom_smooth(method = "lm", se = FALSE, color = "grey") +  # Add regression line
  labs(title = "Gun control laws strength by US states for year 2022",
       x = "Gun control laws strength rank",
       y = "Firearm Mortality rate") +
  scale_color_manual(values = c("most strict" = "green", "strict" = "skyblue", "moderate" = "yellow", "lax" = "orange", "most lax" = "red")) +
  theme_minimal()

# Show legend
scatterplot <- scatterplot + theme(legend.position = "right")

# Show scatterplot with regression line
print(scatterplot)

The analysis of the scatter plot with a regression line, shows a trend where states embracing lax gun laws tend to experience higher firearm-related mortality rate, whereas those with stringent regulations tend to have lower death rates.

The regression line further reinforces the association between stricter gun control laws and reduced firearm-related mortality rate, portraying a general decline in gun violence deaths.

We determined that California had the strongest gun laws in the country. Hawaii topped the list with the lowest rate of gun deaths in the country while Mississippi led the country with both the weakest gun laws and highest rate of gun deaths.

Visualize the Story using heat maps

# Set colors for each gun control laws strength category and Rank
category_colors <- c(
  "most strict" = "green",
  "strict" = "skyblue",
  "moderate" = "yellow",
  "lax" = "orange",
  "most lax" = "red"
)

# Create Plotly heat map
heatmap1 <- plot_geo(final_df, locationmode = 'USA-states') %>%
  add_trace(
    z = final_df$gun_laws_strength_value,
    locations = final_df$State,
    color = final_df$gun_laws_strength_value,
    colors = category_colors,
    text = ~paste("State: ", State, "<br>Category: ", gun_laws_strength_categories,"<br>Death Rate: ", Rate),
    hoverinfo = "text"
  )

# Customize layout
heatmap1 <- heatmap1 %>%
  layout(
    title = "Gun Laws Strength by US States for year 2022",
    geo = list(
      scope = 'usa',
      projection = list(type = 'albers usa'),
      showlakes = TRUE,
      lakecolor = toRGB('white')
    )
  ) 

# Add colorbar legend
heatmap1 <- heatmap1 %>%
  colorbar(
    title = "Gun Laws Strength Category",
    tickvals = 1:5,
    ticktext = c("most strict", "strict", "moderate", "lax", "most lax"),
    ticks = "outside"
  )

# Show heatmap
heatmap1
heat_map2 <- plot_geo(final_df, locations = ~State, text = ~State, z = ~gun_laws_strength_rank) %>%
  add_trace(
    type = "choropleth",
    z = final_df$gun_laws_strength_value,
    locations = final_df$State,
    color = final_df$gun_laws_strength_value,
    colors = category_colors,
    locationmode = "USA-states"
  ) %>%
  colorbar(title = "Gun law Strength (Ranked)") %>%
  layout(
    title = "First-Ranked Stricter Gun Laws Significantly Reduce Firearm Deaths Rate",
    geo = list(
      scope = "usa",
      projection = list(type = "albers usa"),
      showlakes = TRUE,
      lakecolor = toRGB("white")
    ),
    
      list(
        x = 0.05,  # X-coordinate of the note
        y = 0.05,  # Y-coordinate of the note
        xref = "paper",
        yref = "paper",
        text = "Rank is from 1 being an A and 5 being an F",  # Text of the note
        showarrow = FALSE  # Set to TRUE if you want an arrow pointing to the note
      )
    )

# Display the heat map
heat_map2

Conclusions

The story shows that states with most strict gun laws have lower rates of firearm related deaths. Specifically, California is at the top of the rank for gun law strength – a score of 91%, with a low rate of 9.1 gun deaths per 100,000 residents and New Jersey has the second lowest rate of gun deaths in the country with the second ranked gun law strength.

Despite stronger gun laws, some states still feel the effects of being close to a state with more lax regulations. For example, Illinois has the eighth-strongest gun safety laws in the country, but its neighboring state of Indiana has much weaker gun laws.

As a whole, the data visualization indicates that stricter gun control laws play an important role in reducing firearm mortality rates in US states during the year 2022.