crime_and_incarceration_by_state = read_csv("crime_and_incarceration_by_state.csv")
## Rows: 816 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): jurisdiction
## dbl (13): year, prisoner_count, state_population, violent_crime_total, murde...
## lgl (3): includes_jails, crime_reporting_change, crimes_estimated
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
shapefile <- "tl_2023_us_state.shp"
map_data <- st_read(shapefile)
## Reading layer `tl_2023_us_state' from data source
## `/Users/lorenzogurrola/Desktop/COMP 112/Midterm/tl_2023_us_state.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 56 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -179.2311 ymin: -14.60181 xmax: 179.8597 ymax: 71.43979
## Geodetic CRS: NAD83
myData <- crime_and_incarceration_by_state %>%
select(jurisdiction, year, violent_crime_total, state_population) %>%
filter(!jurisdiction == "FEDERAL") %>%
filter(!jurisdiction == "HAWAII") %>%
filter(!jurisdiction== "ALASKA") %>%
filter(!is.na(violent_crime_total)) %>%
filter(year == 2016) %>%
mutate(hundred_thousand_population = state_population/100000) %>%
mutate(v = violent_crime_total/hundred_thousand_population) %>%
mutate(v = round(v, digits = 0)) %>%
mutate(mean_v = mean(v))
newMapData <- map_data %>%
filter(!NAME == "United States Virgin Islands") %>%
filter(!NAME == "Puerto Rico") %>%
filter(!NAME == "Guam") %>%
filter(!NAME == "District of Columbia") %>%
filter(!NAME == "Commonwealth of the Northern Mariana Islands") %>%
filter(!NAME == "American Samoa") %>%
filter(!NAME == "Hawaii") %>%
filter(!NAME == "Alaska") %>%
mutate(NAMECAPITAL = toupper(NAME))
allData <- newMapData %>%
left_join(myData, by = c("NAMECAPITAL" = "jurisdiction"))
leaflet_map <- leaflet(allData) %>%
addTiles() %>%
addPolygons(fillColor = ~colorNumeric("viridis", v)(v),
weight = 1,
color = "white",
smoothFactor = 0.5,
opacity = 1.0,
fillOpacity = 0.7,
highlightOptions = highlightOptions(weight = 2,
color = "white",
fillOpacity = 0.7,
bringToFront = TRUE),
label = ~paste("Violent crimes per 100,000 inhabitants:", v),
labelOptions = labelOptions(style = list("font-weight" = "normal"),
textsize = "13px",
direction = "auto"))
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaflet_map %>% addLegend(
position = "bottomright",
pal = colorNumeric("viridis", allData$v),
values = allData$v,
title = "",
opacity = 1)