From e9e1e72172dfff7675c6e42dc0f5a4facc6d63af Mon Sep 17 00:00:00 2001 From: Reese Hirota <122996073+rhirotacouncil@users.noreply.github.com> Date: Fri, 15 Sep 2023 13:02:04 -0400 Subject: [PATCH] check + move to new folder --- .../01_311_initial_request.Rmd | 10 ++++++++-- .../02_311_interactive.R | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) rename code/{ => 311_committee_requests}/01_311_initial_request.Rmd (97%) rename code/{ => 311_committee_requests}/02_311_interactive.R (86%) diff --git a/code/01_311_initial_request.Rmd b/code/311_committee_requests/01_311_initial_request.Rmd similarity index 97% rename from code/01_311_initial_request.Rmd rename to code/311_committee_requests/01_311_initial_request.Rmd index d81d41f..17e35ff 100644 --- a/code/01_311_initial_request.Rmd +++ b/code/311_committee_requests/01_311_initial_request.Rmd @@ -87,7 +87,7 @@ od_dl_sub <- unique(od_dl_sub) ```{r} # ---- Street cleanliness over time: plot -ggplot(od_dl_sub[od_dl_sub$date_ym < "2023-09-01"], aes(x= date_ym, y=n_comp, color=complaint_type), group=complaint_type) + geom_line() + +ggplot(od_dl_sub, aes(x= date_ym, y=n_comp, color=complaint_type), group=complaint_type) + geom_line() + theme_bw() + labs(x="Date", y="Number of Complaints", title="311 Data on Street Cleanliness Complaints Over Time", subtitle="Jan 2018 - July 2023", colour="Complaint type") + theme_nycc() # ggsave(filename = "clean_over_time.png", path = "visuals", width=10, height=7) @@ -100,6 +100,7 @@ clean_over_time_df <- dcast(melt(as.data.table(od_dl_sub[od_dl_sub$date_ym < "20 id.vars = c("complaint_type", "date_ym")), date_ym + variable ~ complaint_type, value.var = "value") %>% select(date_ym, `Dirty Conditions`, `Missed Collection`, `Sanitation Condition`) +clean_over_time_df # write.csv(clean_over_time_df, "data/output/clean_over_time.csv") ``` @@ -108,6 +109,7 @@ clean_over_time_df <- dcast(melt(as.data.table(od_dl_sub[od_dl_sub$date_ym < "20 clean_by_cd_df <- od_dl[complaint_type %in% c("Dirty Conditions", "Missed Collection", "Sanitation Condition")] %>% group_by(community_board) %>% summarise(count=n()) +clean_by_cd_df # write.csv(clean_by_cd_df, "data/output/clean_by_comm.csv") ``` @@ -122,6 +124,7 @@ dsny_311_id <- dsny_311[dsny_311$complaint_type == "Illegal Dumping",] ```{r} # ---- Illegal dumping over time: table id_over_time_df <- dsny_311_id %>% group_by(date_ym) %>% summarise(n_comp=n()) +id_over_time_df # write.csv(id_over_time_df, "data/output/id_over_time.csv") ``` @@ -134,6 +137,7 @@ ggplot(id_over_time_df[id_over_time_df$date_ym < "2023-09-01",], aes(x= date_ym, ```{r} # ---- Illegal dumping by community district: table id_by_cd_df <- dsny_311_id %>% group_by(community_board) %>% summarise(n_comp=n()) +id_by_cd_df # write.csv(id_by_cd_df, "data/output/id_by_comm.csv") ``` @@ -147,6 +151,7 @@ dsny_311_dv <- dsny_311[dsny_311$complaint_type == "Derelict Vehicles",] ```{r} # ---- Derelict vehicles over time: table dv_over_time_df <- dsny_311_dv %>% group_by(date_ym) %>% summarise(n_comp=n()) +dv_over_time_df # write.csv(dv_over_time_df, "data/output/dv_over_time.csv") ``` @@ -159,6 +164,7 @@ ggplot(dv_over_time_df[dv_over_time_df$date_ym < "2023-09-01",], aes(x= date_ym, ```{r} # ---- Derelict vehicles by community district: table dv_by_cd_df <- dsny_311_dv %>% group_by(community_board) %>% summarise(n_comp=n()) +dv_by_cd_df # write.csv(dv_over_time_df, "data/output/dv_by_comm.csv") ``` @@ -204,7 +210,7 @@ cb_by_cd_df <- dep_311_cb %>% group_by(community_board) %>% summarise(n_comp=n() ```{r} dep_311_grease <- dep_311[grepl("grease", dep_311$descriptor, ignore.case = TRUE),] -# dep_311_grease +dep_311_grease # write.csv(dep_311_grease, "data/output/all_grease_data.csv") ``` diff --git a/code/02_311_interactive.R b/code/311_committee_requests/02_311_interactive.R similarity index 86% rename from code/02_311_interactive.R rename to code/311_committee_requests/02_311_interactive.R index ee4287e..b2de07c 100644 --- a/code/02_311_interactive.R +++ b/code/311_committee_requests/02_311_interactive.R @@ -2,8 +2,22 @@ library(tidyverse) library(htmltools) library(ggiraph) library(zoo) +library(readr) +library(data.table) +library(councildown) -# run 01_311_inital_request.Rmd first +# accompanying interactive plots to 01_311_initial_request.Rmd +# you don't have to run the first file if you just want the interactive plots + +dsny_311 <- fread("https://data.cityofnewyork.us/resource/fhrw-4uyv.csv?agency='DSNY'&$where=created_date>='2018-01-01'&$limit=999999999999") +setDT(dsny_311) +dsny_311[, created_date := as.Date(created_date)][, yr := year(created_date)][, mo := month(created_date)] +dsny_311[, date_ym:=as.yearmon(created_date)] + +dep_311 <- fread("https://data.cityofnewyork.us/resource/fhrw-4uyv.csv?agency='DEP'&$where=created_date>='2018-01-01'&$limit=999999999999") +setDT(dep_311) +dep_311[, created_date := as.Date(created_date)][, yr := year(created_date)][, mo := month(created_date)] +dep_311[, date_ym:=as.yearmon(created_date)] # ---- street cleanliness od_dl <- dsny_311 @@ -11,7 +25,7 @@ od_dl[complaint_type %in% c("Overflowing Litter Baskets", "Dirty Condition", "Di od_dl[grepl("Litter", complaint_type, ignore.case = TRUE) | grepl("litter", descriptor, ignore.case = TRUE), complaint_type := "Litter"] od_dl_sub <- od_dl[complaint_type %in% c("Dirty Conditions", "Litter")] # > sum((od_dl_sub[od_dl_sub$date_ym > "2022-08-01" & od_dl_sub$complaint_type == "Dirty Conditions",])$n_comp) -# [1] 47537 - from old version, littering adds ~10k +# [1] 47537 - from old version, littering adds ~3k od_dl_sub[, n_comp := .N, by = c("complaint_type", "date_ym")] # count by complaint type, date od_dl_sub <- od_dl_sub[, .(complaint_type, date_ym, n_comp)] # subset to complaint type, date, n @@ -114,6 +128,7 @@ id_interactive <- girafe(ggobj = id_plot, )) # htmltools::save_html(id_interactive, "../visuals/id_complaints_citywide_line.html") +dep_311_grease <- dep_311[grepl("grease", dep_311$descriptor, ignore.case = TRUE),] # yellow/brown grease grease_over_time_df <- dep_311_grease[dep_311_grease$date_ym >= "2022-08-01" & dep_311_grease$date_ym <= "2023-08-01",] %>% group_by(date_ym) %>%