added cumulative crash graph

This commit is contained in:
Ben Varick 2025-09-24 12:06:57 -07:00
parent ac3decc6fb
commit cf85f8b2a8
Signed by: ben
SSH key fingerprint: SHA256:jWnpFDAcacYM5aPFpYRqlsamlDyKNpSj3jj+k4ojtUo

View file

@ -27,10 +27,7 @@ library(ggnewscale)
library(RColorBrewer) library(RColorBrewer)
library(magick) library(magick)
library(rsvg) library(rsvg)
library(parallel)
library(tidycensus) library(tidycensus)
library(MASS)
library(raster)
``` ```
@ -145,7 +142,8 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "month_age.png"),
create.dir = TRUE) create.dir = TRUE)
ggplot(data = TOPS_data_filtered %>% ggplot(data = TOPS_data_filtered %>%
filter(vulnerable_role == "Pedestrian", filter(
#vulnerable_role == "Pedestrian",
month(date) <= 8, month(date) <= 8,
) %>% ) %>%
# filter(ped_age < 18, # filter(ped_age < 18,
@ -156,7 +154,7 @@ ggplot(data = TOPS_data_filtered %>%
y = total), y = total),
fill = "lightblue4") + fill = "lightblue4") +
scale_y_continuous(expand = expansion(mult = c(0,0.1))) + 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"), subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"),
x = NULL, x = NULL,
y = "Crashes per year", y = "Crashes per year",
@ -164,7 +162,7 @@ ggplot(data = TOPS_data_filtered %>%
strftime(retrieve_date, format = "%m/%Y"), strftime(retrieve_date, format = "%m/%Y"),
"\nper direction of the WisDOT Bureau of Transportation Safety")) + "\nper direction of the WisDOT Bureau of Transportation Safety")) +
theme(plot.caption = element_text(color = "grey")) 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, device = png,
height = 8.5, height = 8.5,
width = 11, width = 11,
@ -172,7 +170,38 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "ped_years.png"),
create.dir = TRUE) create.dir = TRUE)
ggplot(data = TOPS_data_filtered %>% 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) %>% month(date) <= 8) %>%
mutate(mke_city = factor(ifelse(MUNINAME %in% "MILWAUKEE", "City of Milwaukee", "Other municipalities"), mutate(mke_city = factor(ifelse(MUNINAME %in% "MILWAUKEE", "City of Milwaukee", "Other municipalities"),
levels = c("Other municipalities", "City of Milwaukee"))) %>% levels = c("Other municipalities", "City of Milwaukee"))) %>%
@ -183,7 +212,7 @@ ggplot(data = TOPS_data_filtered %>%
fill = mke_city), fill = mke_city),
position = position_dodge()) + position = position_dodge()) +
scale_y_continuous(expand = expansion(mult = c(0,0.1))) + 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"), subtitle = paste0(str_to_title(focus_county), " County | ", "January - August"),
x = NULL, x = NULL,
y = "Crashes", y = "Crashes",
@ -199,6 +228,40 @@ ggsave(filename = paste0("figures/MilWALKee_Walks/", "ped_years_MKEcity.png"),
units = "in", units = "in",
create.dir = TRUE) 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 ## Milwaukee maps