Created
September 9, 2022 15:18
-
-
Save mfherman/892d8e3861c36a305ec3c8da885d3d2c to your computer and use it in GitHub Desktop.
Plot violent crime trends from Omaha PD 2016-2021 and compare to 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(lubridate) | |
library(janitor) | |
# get urls of csv incident files to download for year 2015 - 2022 | |
urls <- map_chr(2015:2022, ~ paste0("https://police-static.cityofomaha.org/crime-data/", | |
.x, "/Incidents_", .x, ".csv")) | |
# download all years, combine into one df and clean up variable names | |
incidents <- read_csv(urls) |> | |
clean_names() | |
# deduplicate incidents, create month, year vars, new var for combining violent crimes | |
incidents_clean <- incidents |> | |
distinct(rb_number, reported_date, reported_time, | |
statute_ordinance_description, occurred_location, .keep_all = TRUE) |> | |
mutate( | |
reported_date = mdy(reported_date), | |
year = year(reported_date), | |
month = month(reported_date), | |
violent_crime = case_when( | |
statute_ordinance_description == "HOMICIDE" ~ "Homicides", | |
statute_ordinance_description %in% c("ASSAULT - FELONY - AGGRAVATED", | |
"ASSAULT - FELONY - AGGRAVATED -ATTEMPT") ~ "Agg Assaults and Attemped Agg Assaults", | |
str_detect(statute_ordinance_description, "ROBBERY") ~ "Robberies and Attempted Robberies", | |
) | |
) |> | |
filter(year >= 2016) | |
# for 2022, count number of violent crimes by month | |
violent_crime_2022 <- incidents_clean |> | |
filter(!is.na(violent_crime), year == 2022, month <= 8) |> | |
count(violent_crime, month) | |
# for each month from 2016-2021, calc mean, min, and max of each violent crime | |
by_month <- incidents_clean |> | |
filter(!is.na(violent_crime), year <= 2021) |> | |
count(violent_crime, year, month) |> | |
group_by(violent_crime, month) |> | |
summarize( | |
mean = mean(n), | |
min = min(n), | |
max = max(n) | |
) |> | |
ungroup() | |
# plot - line for 2022, dashed line for 2016-21 mean and filled range for 2016-21 min/max | |
by_month |> | |
ggplot(aes(month)) + | |
geom_ribbon(aes(ymin = min, ymax = max, fill = "c"), alpha = 0.1) + | |
geom_line(aes(y = mean, color = "a"), linetype = "dashed") + | |
geom_line(data = violent_crime_2022, aes(y = n, color = "b"), lwd = 1) + | |
scale_x_continuous(breaks = 1:12) + | |
scale_fill_manual(labels = "Min/Max 2016-21", values = c(c = "black")) + | |
scale_color_manual(labels = c("Mean 2016-21", "2022"), | |
values = c(a = "black", b = "red")) + | |
ylim(0, NA) + | |
facet_wrap(vars(violent_crime), scales = "free", nrow = 2) + | |
labs( | |
x = "Month", | |
y = NULL, | |
color = NULL, | |
fill = NULL | |
) + | |
theme_minimal() + | |
theme( | |
plot.margin = margin(20, 20, 20, 20), | |
panel.grid.minor = element_blank(), | |
legend.position = c(0.8, 0.2) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an R replication of Justin Nix's Stata code to see how a recent month of crime data compares to prior years and create the following plot.