From 8e86ece8c31fa9aa5d3c67412d192098ccb88909 Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Thu, 9 Oct 2025 10:25:54 -0700 Subject: [PATCH] edited MilWALKeeWalks.Rmd to make months more dynamic --- R/MilWALKeeWalks.Rmd | 58 +++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/R/MilWALKeeWalks.Rmd b/R/MilWALKeeWalks.Rmd index 37d0615..a8a14ef 100644 --- a/R/MilWALKeeWalks.Rmd +++ b/R/MilWALKeeWalks.Rmd @@ -49,6 +49,8 @@ TOPS_data_filtered <- TOPS_data %>% filter(CNTYNAME == focus_county) ```{r startenddates, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} year_min <- min(year(TOPS_data_filtered$date)) year_max <- max(year(TOPS_data_filtered$date)) +month_end <- 9 +date_max <- ymd(paste(year_max, month_end + 1,1)) - 1 ``` ## intro charts @@ -144,7 +146,7 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "month_age.png"), ggplot(data = TOPS_data_filtered %>% filter( #vulnerable_role == "Pedestrian", - month(date) <= 8, + month(date) <= month_end, ) %>% # filter(ped_age < 18, # MUNINAME %in% "MILWAUKEE") %>% @@ -155,7 +157,7 @@ ggplot(data = TOPS_data_filtered %>% fill = "lightblue4") + scale_y_continuous(expand = expansion(mult = c(0,0.1))) + labs(title = paste0("Car crashes involving pedestrians & bicyclists"), - subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), + subtitle = paste0(str_to_title(focus_county), " County | ", "January - ", format(ymd(paste(2025, month_end, 01)), "%B")), x = NULL, y = "Crashes per year", caption = paste0("crash data from UW TOPS lab - retrieved ", @@ -172,7 +174,7 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "vuln_years.png"), ggplot(data = TOPS_data_filtered %>% filter( #vulnerable_role == "Pedestrian", - month(date) <= 8, + month(date) <= month_end, ped_inj %in% c("K", "A"), ) %>% group_by(year, ped_inj_name) %>% @@ -184,7 +186,7 @@ ggplot(data = TOPS_data_filtered %>% scale_y_continuous(expand = expansion(mult = c(0,0.1))) + scale_fill_manual(values = setNames(injury_severity$color, injury_severity$InjSevName), name = "Injury severity") + labs(title = paste0("Car crashes involving pedestrians & bicyclists - fatal and serious injuries"), - subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), + subtitle = paste0(str_to_title(focus_county), " County | ", "January - ", format(ymd(paste(2025, month_end, 01)), "%B")), x = NULL, y = "Crashes per year", caption = paste0("crash data from UW TOPS lab - retrieved ", @@ -202,7 +204,7 @@ ggplot(data = TOPS_data_filtered %>% filter( #vulnerable_role == "Pedestrian", ped_inj %in% c("K", "A"), - month(date) <= 8) %>% + month(date) <= month_end) %>% mutate(mke_city = factor(ifelse(MUNINAME %in% "MILWAUKEE", "City of Milwaukee", "Other municipalities"), levels = c("Other municipalities", "City of Milwaukee"))) %>% group_by(year, mke_city) %>% @@ -213,7 +215,7 @@ ggplot(data = TOPS_data_filtered %>% position = position_dodge()) + scale_y_continuous(expand = expansion(mult = c(0,0.1))) + labs(title = paste0("Car crashes involving pedestrians - fatal and severe injuries"), - subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), + subtitle = paste0(str_to_title(focus_county), " County | ", "January - ", format(ymd(paste(2025, month_end, 01)), "%B")), x = NULL, y = "Crashes", fill = NULL, @@ -293,7 +295,7 @@ hexgrid <- rowid_to_column(st_transform(st_as_sf(st_make_grid(st_transform(count what = 'polygons', square = FALSE )), crs = 4326), "ID") -yearsforprior <- 7 +yearsforprior <- year_max - year_min - 1 hex_crashes <- st_join(hexgrid, st_as_sf(TOPS_data_filtered %>% filter(vulnerable_role %in% "Pedestrian") %>% @@ -303,8 +305,8 @@ hex_crashes <- st_join(hexgrid, crs = 4326), join = st_contains) %>% filter(!is.na(year)) %>% - filter(date > (max(date) - (365 * (yearsforprior + 1)))) %>% - mutate(lastyear = ifelse((date <= max(date) - 365), + filter(date > (date_max - (365 * (yearsforprior + 1)))) %>% + mutate(lastyear = ifelse((date <= date_max - 365), "prior", "lastyear")) %>% group_by(ID, lastyear) %>% @@ -340,8 +342,8 @@ hex_crashes_small <- st_join(hexgrid_small, crs = 4326), join = st_contains) %>% filter(!is.na(year)) %>% - filter(date > (max(date) - (365 * (yearsforprior + 1)))) %>% - mutate(lastyear = ifelse((date <= max(date) - 365), + filter(date > (date_max - (365 * (yearsforprior + 1)))) %>% + mutate(lastyear = ifelse((date <= date_max - 365), "prior", "lastyear")) %>% group_by(ID, lastyear) %>% @@ -365,7 +367,7 @@ hex_crashes_small_points <- st_centroid(hex_crashes_small) ```{r previousyearstring, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} -previousyearstring <- paste0(format(max(TOPS_data_filtered$date, na.rm = TRUE) - 365 + 1, "%m/%Y"), " - ", format(max(TOPS_data_filtered$date, na.rm = TRUE), "%m/%Y")) +previousyearstring <- paste0(format(date_max - 365 + 1, "%m/%Y"), " - ", format(date_max, "%m/%Y")) ``` ```{r importprojects, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} @@ -388,9 +390,9 @@ ggmap(basemap) + labs(title = paste0("Car crashes involving pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - year(min(TOPS_data_filtered$date, na.rm = TRUE)), + year_min, " - ", - year(max(TOPS_data_filtered$date, na.rm = TRUE))), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety", @@ -479,9 +481,9 @@ ggmap(basemap) + labs(title = paste0("Car crashes involving pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - min(year(TOPS_data$date), na.rm = TRUE), + year_min, " - ", - max(year(TOPS_data$date), na.rm = TRUE)), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety", @@ -533,9 +535,9 @@ ggmap(basemap) + labs(title = paste0("Car crashes involving pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - min(year(TOPS_data$date), na.rm = TRUE), + year_min, " - ", - max(year(TOPS_data$date), na.rm = TRUE)), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety", @@ -566,7 +568,7 @@ ggmap(basemap) + # add crash locations new_scale_fill() + geom_point(data = TOPS_data_filtered %>% - filter(date > max(TOPS_data_filtered$date) - 365) %>% + filter(date > date_max - 365) %>% #filter(ped_inj %in% c("K", "A", "B")) %>% filter(vulnerable_role %in% "Pedestrian") %>% filter(longitude >= as.double(bbox[1]), @@ -591,7 +593,7 @@ ggsave(file = paste0("figures/MilWALKee_Walks/", ## compare crashes in area nrow(TOPS_data_filtered %>% - filter(date > max(TOPS_data_filtered$date) - 365) %>% + filter(date > date_max - 365) %>% filter(ped_inj %in% c("K", "A")) %>% filter(vulnerable_role %in% "Pedestrian") %>% filter(longitude >= as.double(bbox[1]), @@ -599,7 +601,7 @@ nrow(TOPS_data_filtered %>% longitude <= as.double(bbox[3]), latitude <= as.double(bbox[4]))) nrow(TOPS_data_filtered %>% - filter(date > (max(TOPS_data_filtered$date) - 365 * (yearsforprior + 1))) %>% + filter(date > (date_max - 365 * (yearsforprior + 1))) %>% filter(ped_inj %in% c("K", "A")) %>% filter(vulnerable_role %in% "Pedestrian") %>% filter(longitude >= as.double(bbox[1]), @@ -617,9 +619,9 @@ ggmap(basemap) + labs(title = paste0("Crashes between cars and pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - min(year(TOPS_data$date), na.rm = TRUE), + year_min, " - ", - max(year(TOPS_data$date), na.rm = TRUE)), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety", @@ -658,9 +660,9 @@ ggmap(basemap) + labs(title = paste0("Crashes between cars and pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - min(year(TOPS_data$date), na.rm = TRUE), + year_min, " - ", - max(year(TOPS_data$date), na.rm = TRUE)), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety", @@ -694,7 +696,7 @@ ggmap(basemap) + ## identify Halloween trick-or-treating days ```{r trickortreatdays, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} -trickortreatdays <- data_frame(year = seq(year(min(TOPS_data$date)), year(max(TOPS_data$date)), 1)) +trickortreatdays <- data_frame(year = seq(year_min, year_max, 1)) trickortreatdays <- trickortreatdays %>% mutate(halloween = ymd(paste(year, "10, 31"))) %>% mutate(wday = wday(halloween, label = TRUE)) %>% @@ -879,9 +881,9 @@ ggmap(basemap) + labs(title = paste0("Car crashes involving pedestrians"), subtitle = paste0(str_to_title(focus_county), " County | ", - min(year(TOPS_data$date), na.rm = TRUE), + year_min, " - ", - max(year(TOPS_data$date), na.rm = TRUE)), + year_max), caption = paste0("crash data from UW TOPS lab - retrieved ", strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety",