From cf85f8b2a86a632543bb898766cd740e97387ffa Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Wed, 24 Sep 2025 12:06:57 -0700 Subject: [PATCH] added cumulative crash graph --- R/MilWALKeeWalks.Rmd | 79 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/R/MilWALKeeWalks.Rmd b/R/MilWALKeeWalks.Rmd index 8d702bf..0f1a421 100644 --- a/R/MilWALKeeWalks.Rmd +++ b/R/MilWALKeeWalks.Rmd @@ -27,10 +27,7 @@ library(ggnewscale) library(RColorBrewer) library(magick) library(rsvg) -library(parallel) library(tidycensus) -library(MASS) -library(raster) ``` @@ -145,7 +142,8 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "month_age.png"), create.dir = TRUE) ggplot(data = TOPS_data_filtered %>% - filter(vulnerable_role == "Pedestrian", + filter( + #vulnerable_role == "Pedestrian", month(date) <= 8, ) %>% # filter(ped_age < 18, @@ -156,7 +154,7 @@ ggplot(data = TOPS_data_filtered %>% y = total), fill = "lightblue4") + scale_y_continuous(expand = expansion(mult = c(0,0.1))) + - labs(title = paste0("Crashes involved pedestrians"), + labs(title = paste0("Crashes involved pedestrians & bicyclists"), subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), x = NULL, y = "Crashes per year", @@ -164,7 +162,7 @@ ggplot(data = TOPS_data_filtered %>% strftime(retrieve_date, format = "%m/%Y"), "\nper direction of the WisDOT Bureau of Transportation Safety")) + theme(plot.caption = element_text(color = "grey")) -ggsave(filename = paste0("figures/MilWALKee_Walks/", "ped_years.png"), +ggsave(filename = paste0("figures/MilWALKee_Walks/", "vuln_years.png"), device = png, height = 8.5, width = 11, @@ -172,7 +170,38 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "ped_years.png"), create.dir = TRUE) ggplot(data = TOPS_data_filtered %>% - filter(vulnerable_role == "Pedestrian", + filter( + #vulnerable_role == "Pedestrian", + month(date) <= 8, + ped_inj %in% c("K", "A"), + ) %>% + group_by(year, ped_inj_name) %>% + summarize(total = n())) + + geom_col(aes(x = year, + y = total, + fill = ped_inj_name), + position = position_dodge()) + + 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("Crashes involved pedestrians & bicyclists - fatal and serious injuries"), + subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), + x = NULL, + y = "Crashes per year", + caption = paste0("crash data from UW TOPS lab - retrieved ", + strftime(retrieve_date, format = "%m/%Y"), + "\nper direction of the WisDOT Bureau of Transportation Safety")) + + theme(plot.caption = element_text(color = "grey")) +ggsave(filename = paste0("figures/MilWALKee_Walks/", "vuln_years_severe.png"), + device = png, + height = 8.5, + width = 11, + units = "in", + create.dir = TRUE) + +ggplot(data = TOPS_data_filtered %>% + filter( + #vulnerable_role == "Pedestrian", + ped_inj %in% c("K", "A"), month(date) <= 8) %>% mutate(mke_city = factor(ifelse(MUNINAME %in% "MILWAUKEE", "City of Milwaukee", "Other municipalities"), levels = c("Other municipalities", "City of Milwaukee"))) %>% @@ -183,7 +212,7 @@ ggplot(data = TOPS_data_filtered %>% fill = mke_city), position = position_dodge()) + scale_y_continuous(expand = expansion(mult = c(0,0.1))) + - labs(title = paste0("Crashes involved pedestrians"), + labs(title = paste0("Crashes involved pedestrians - fatal and severe injuries"), subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"), x = NULL, y = "Crashes", @@ -199,6 +228,40 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "ped_years_MKEcity.png"), units = "in", create.dir = TRUE) +ggplot(data = TOPS_data_filtered %>% + filter( + #vulnerable_role == "Pedestrian", + # month(date) <= 8, + ped_inj %in% c("K", "A"), + ) %>% + group_by(year, month) %>% + summarize(total = n()) %>% + arrange(year, month) %>% + group_by(year) %>% + mutate(cumcrashes = cumsum(total))) + + geom_line(aes(x = month, + y = cumcrashes, + group = year, + color = year)) + + scale_y_continuous(expand = expansion(mult = c(0,0.1))) + + scale_color_brewer(palette = "Set1") + + scale_fill_manual(values = setNames(injury_severity$color, injury_severity$InjSevName), name = "Injury severity") + +labs(title = paste0("Crashes involved pedestrians & bicyclists - fatal and serious injuries"), + subtitle = paste0(str_to_title(focus_county), " County"), + x = NULL, + y = "Cumulative crashes", + color = NULL, + caption = paste0("crash data from UW TOPS lab - retrieved ", + strftime(retrieve_date, format = "%m/%Y"), + "\nper direction of the WisDOT Bureau of Transportation Safety")) + + theme(plot.caption = element_text(color = "grey")) +ggsave(filename = paste0("figures/MilWALKee_Walks/", "vuln_years_cumulative_severe.png"), + device = png, + height = 8.5, + width = 11, + units = "in", + create.dir = TRUE) + ``` ## Milwaukee maps