Skip to content

Instantly share code, notes, and snippets.

@tor-gu
Last active March 17, 2022 16:03
Show Gist options
  • Save tor-gu/ea2c6802b57dbcf4d1dfdc6238d089cd to your computer and use it in GitHub Desktop.
Save tor-gu/ea2c6802b57dbcf4d1dfdc6238d089cd to your computer and use it in GitHub Desktop.
Per capita use of force by type in NJ counties
library(tidyverse)
# Get number of incidents of selected types of force by county
# devtools::install_github("tor-gu/njoaguof")
library(njoaguof)
force_type_by_county <- incident %>%
right_join(incident_subject_force_type) %>%
mutate(
force_type =
fct_collapse(
force_type,
other_level = "Other",
"Chemical Spray" = c("Discharged Chemical at", "High Volume OC Spray"),
"Conducted Energy Device" = "Used CED on",
"Dog bite" = c("Canine bit (apprehension)", "Canine bit (spontaneous)"),
"Firearm discharge"="Discharged Firearm at",
)
) %>%
filter(force_type != "Other") %>%
filter(incident_date_1 < "2022-02-01") %>%
mutate(force_type = fct_drop(force_type, "Other")) %>%
select(form_id, county = incident_municipality_county, force_type) %>%
group_by(county) %>% count(force_type, .drop = FALSE)
# Get New Jersey county population data from the US Census
library(tidycensus)
county_pop <- get_estimates(geography = "county", product = "population") %>%
separate(NAME, sep=", ", into=c("county", "state")) %>%
filter(state=="New Jersey", variable=="POP") %>%
mutate(county=str_remove(county, " County")) %>%
select(county, population=value)
# Combine the use of force data with the census data
force_by_percap <- force_type_by_county %>% right_join(county_pop) %>%
mutate(per100k=n*100000/population) %>%
select(county, force_type, per100k)
# Get the New Jersey county map and plot
library(tigris)
options(tigris_use_cache=TRUE)
county_map <- counties(state="New Jersey", class="sf")
library(tmap)
county_map %>%
geo_join(force_by_percap, by_sp="NAME", by_df="county", how="inner") %>%
tm_shape() +
tm_polygons("per100k", title="Per 100k", style="cont") +
tm_facets("force_type", free.scales = TRUE) +
tm_layout(main.title="NJ police use of force per capita\nOct 2020-Jan 2022") +
tm_credits("US Census\nNJ OAG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment