Introduction

Gun violence is common in the United States and its trends can be shown through data visualization. The data presented is the reported crimes of gun violence in the United States from 2013 to 2018.

Data

The data is originally from http://www.gunviolencearchive.org/ provided by the Gun Violence Archive. I downloaded the dataset from https://www.kaggle.com/jameslko/gun-violence-data providing it in a .csv format.

GunViolence <-read.csv("gun-violence-data_01-2013_03-2018.csv")

This shows how the data is catagorized. When creating the map, we can display different variables like the age of participant or the gun type used.

str(GunViolence)
## 'data.frame':    239677 obs. of  29 variables:
##  $ incident_id                : int  461105 460726 478855 478925 478959 478948 479363 479374 479389 492151 ...
##  $ date                       : Factor w/ 1725 levels "2013-01-01","2013-01-05",..: 1 1 1 2 3 3 4 5 5 6 ...
##  $ state                      : Factor w/ 51 levels "Alabama","Alaska",..: 39 5 36 6 34 37 32 19 5 21 ...
##  $ city_or_county             : Factor w/ 12898 levels "Abbeville","Abbotsford",..: 7178 4949 6607 451 4614 11730 89 8062 1260 531 ...
##  $ address                    : Factor w/ 198038 levels ""," 100 block of Kohler Court",..: 29736 23158 36801 32939 72054 113527 64984 167378 10819 29165 ...
##  $ n_killed                   : int  0 1 1 4 2 4 5 0 0 1 ...
##  $ n_injured                  : int  4 3 3 0 2 0 0 5 4 6 ...
##  $ incident_url               : Factor w/ 239677 levels "http://www.gunviolencearchive.org/incident/1000004",..: 116458 116373 120598 120605 120614 120612 120701 120704 120707 123641 ...
##  $ source_url                 : Factor w/ 213990 levels "","/%20---8newsnow.com",..: 145055 71228 12462 71471 90111 38112 18393 133646 33633 51836 ...
##  $ incident_url_fields_missing: Factor w/ 1 level "False": 1 1 1 1 1 1 1 1 1 1 ...
##  $ congressional_district     : int  14 43 9 6 6 1 1 2 9 7 ...
##  $ gun_stolen                 : Factor w/ 350 levels "","0::Not-stolen",..: 1 1 214 1 214 1 214 1 1 1 ...
##  $ gun_type                   : Factor w/ 2503 levels "","0::10mm","0::10mm||1::22 LR",..: 1 1 2347 1 1481 1 162 1 1 1 ...
##  $ incident_characteristics   : Factor w/ 18127 levels "","Accidental Shooting",..: 12179 14081 14488 8285 14653 7374 7854 10883 10886 14051 ...
##  $ latitude                   : num  40.3 33.9 41.4 39.7 36.1 ...
##  $ location_description       : Factor w/ 27596 levels "","'Taste' Dessert Bar",..: 1 1 6169 1 1 8549 1 1 1 1 ...
##  $ longitude                  : num  -79.9 -118.3 -82.1 -104.8 -80 ...
##  $ n_guns_involved            : int  NA NA 2 NA 2 NA 2 NA NA NA ...
##  $ notes                      : Factor w/ 136653 levels "","' When asked what was going on, Rongstad stated he was â\200œjust protecting his countryâ\200\235 and was â\200"| __truncated__,..: 59363 48798 1 1 124754 1 1 126262 92130 102279 ...
##  $ participant_age            : Factor w/ 18952 levels "","0::0","0::0||1::1||2::28||3::24",..: 2693 2693 5692 7404 1959 4538 12867 1 1 598 ...
##  $ participant_age_group      : Factor w/ 899 levels "","0::Adult 18+",..: 7 6 7 6 191 8 185 1 645 527 ...
##  $ participant_gender         : Factor w/ 874 levels "","0::Female",..: 554 233 462 143 128 18 287 462 462 233 ...
##  $ participant_name           : Factor w/ 113489 levels "","0::\"Bear\"||1::Cardell Monroe",..: 50691 8143 18775 82962 19795 74096 34749 1 1 25150 ...
##  $ participant_relationship   : Factor w/ 285 levels "","0::Aquaintance",..: 1 1 1 1 218 1 267 1 1 1 ...
##  $ participant_status         : Factor w/ 2151 levels "","0::Arrested",..: 42 782 132 1157 375 1197 1194 238 306 806 ...
##  $ participant_type           : Factor w/ 260 levels "","0::Subject-Suspect",..: 138 138 55 123 123 139 148 148 138 165 ...
##  $ sources                    : Factor w/ 217281 levels "","http://1160wccs.com/blairsville-woman-robbed/",..: 32540 26102 119001 14108 28453 97759 63213 87266 70210 4577 ...
##  $ state_house_district       : int  NA 62 56 40 62 72 10 93 11 NA ...
##  $ state_senate_district      : int  NA 35 13 28 27 11 14 5 7 44 ...
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.3
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(RColorBrewer)

Nationwide Map

Because there are so many cases of gun violence throughout the United States, it is difficult to find trends when looking at it as a whole.

GunViolence %>%
  leaflet() %>%
  addTiles() %>%
addMarkers(popup=GunViolence$gun_type , clusterOptions=markerClusterOptions())
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 7923 rows with
## either missing or invalid lat/lon values and will be ignored

Statewide Map

As a solution, we can divide the data by the state in which the crime occured. In this case, I used Maine since it has one of the lowest crime rates in the U.S. We can see that there are more shootings in densely populated cities like Portland. It is also interesting to note the types of guns used. For example, rifles are used more often than expected.

Mochi <- filter(GunViolence, state == "Maine")

The colors of the markers can also be adjusted.

pal <- colorFactor(
palette = 'Spectral',
domain = Mochi$gun_type
)
Mochi %>%
  leaflet() %>%
  addTiles() %>%
addCircleMarkers(popup=Mochi$gun_type , clusterOptions=markerClusterOptions(), color = ~pal(gun_type))
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Spectral is 11
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Spectral is 11
## Returning the palette you asked for with that many colors

Conclusion

By using Leaflet to create an interactive map, we can look for trends in gun violence throughout the United States by location as well as details of the crime. This assignments shows how packages like Leaflet allow users to visualize data without using JavaScript.