2 min read

Milwaukee Traffic Accidents

I’m currently developing an R package to assist in downloading, cleaning, and geocoding data from the City of Milwaukee’s newish open data portal at data.milwaukee.gov.

Here’s an example of how to use it to build a grapsh of traffic accidents by time of day for each day of the week.

library(tidyr)
library(ggplot2)
devtools::install_github("jdjohn215/milwaukeer")
library(milwaukeer)

# Download the data
accidents <- get_TrafficAccidents()
## [1] "Download time: 2.68 seconds."
## [1] "5 Incidents are missing properly formatted dates. Set include_missingDate = TRUE to include them."
# create hour, minute, and second variables
# use lubridate::wday to find day of the week, setting start day to Monday
by.day.time <- accidents %>%
  separate(col = "time", sep = ":", into = c("hour","minute","second"),
           convert = TRUE) %>%
  mutate(DayOfWeek = lubridate::wday(date, label = TRUE,
                                     week_start = 1)) %>%
  group_by(hour, minute, DayOfWeek) %>%
  mutate(CumMinute = (hour*60) + minute)

# Make a vector of the minute of the day which marks the beginning of each hour
# This will be used to make the x-axis breaks
hours <- seq(from = 0, to = 1380, by = 60)

# Make the plot
# try running it in short chunks to see what each part does
ggplot(by.day.time, aes(CumMinute)) +
  geom_vline(xintercept = hours, col = "grey",
             size = 0.2, linetype = "dotted") +
  geom_histogram(binwidth = 20) +
  geom_hline(yintercept = c(0, 150, 300, 450, 650),
             color = "white", size = 0.2) +
  facet_wrap(facets = vars(DayOfWeek)) +
  scale_x_continuous(breaks = hours,
                     labels = c("12am","","2","","4","",
                                "6","","8","","10","",
                                "noon","","2","","4","",
                                "6","","8","","10","")) +
  labs(title = "Traffic Accidents in Milwaukee by time of day and day of week",
       subtitle = "accidents are binned in 20 minute intervals",
       caption = "Source: City of Milwaukee. Data from 2007 to 2019") +
  ggthemes::theme_tufte() +
  theme(axis.text.x = element_text(angle = -45, hjust = 0),
        axis.ticks = element_blank(),
        axis.text.y = element_blank(),
        axis.title = element_blank())