Skip to content

Instantly share code, notes, and snippets.

@alexeyknorre
Created June 14, 2023 14:12
Show Gist options
  • Save alexeyknorre/bf1e1bc7302d00b1f18a8a435f5e466b to your computer and use it in GitHub Desktop.
Save alexeyknorre/bf1e1bc7302d00b1f18a8a435f5e466b to your computer and use it in GitHub Desktop.
Monthly averages for shootings in NYC (2013-2022)
library(tidyverse)
library(ggrepel)
ny_sh <- read.csv("data/NYPD_Shooting_Incident_Data__Historic_.csv")
ny_sh_prep <- ny_sh %>%
mutate(date = mdy(OCCUR_DATE),
year = year(date),
month = month(date,
label = T,
abbr = F),
type = ifelse(STATISTICAL_MURDER_FLAG == "true",
"Fatal shootings",
"Nonfatal shootings"))
nyc_sh_ym <- ny_sh_prep %>%
filter(year > 2012) %>%
group_by(month, type, year) %>%
summarise(count = n())
nyc_sh_ym_mean <- nyc_sh_ym %>%
group_by(month, type) %>%
summarise(mean = round(mean(count),1)) %>%
mutate(year = "average")
ggplot(nyc_sh_ym_mean, aes(x = month,
y = mean,
group = type,
color = type)) +
geom_point() +
geom_text_repel(aes(label = mean),
color = "black",
nudge_y = .2,size = 3) +
geom_line() +
theme_minimal() +
labs(x="Average for the month",
y = "Number of victims",
color = "Type",
title = "Monthly averages for shooting victims in NYC (2013-2022)",
caption = "Data: NYPD Shooting Incident Data (Historic)\nhttps://data.cityofnewyork.us/Public-Safety/NYPD-Shooting-Incident-Data-Historic-/833y-fsy8") +
theme(legend.position = "bottom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment