Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Construct a map of traffic accident date vs time similar to:
- ### https://www.reddit.com/r/dataisbeautiful/comments/i0a91n/car_crashes_by_time_and_day_chicago_oc/
- ### Data Source: https://data.nashville.gov/browse?category=Public%20Safety
- library(tidyverse)
- library(janitor)
- library(lubridate)
- data <-
- "Nashville/Traffic_Accidents/Traffic_Accidents__2020_.csv" %>%
- read_csv() %>%
- janitor::clean_names()
- select(date_and_time) %>%
- filter(!is.na(date_and_time)) %>%
- mutate(date_and_time = mdy_hms(date_and_time)) %>%
- mutate(hour = hour(date_and_time)) %>%
- mutate(dow = wday(date_and_time)) %>%
- group_by(hour, dow) %>%
- summarize(wrecks = n())
- g_wrecks_hour_dow <-
- ggplot(data = data, aes(x = hour, y = dow, fill = wrecks)) +
- theme_minimal() +
- theme(legend.position = "none") +
- theme(plot.title = element_text(hjust = 0.5)) +
- theme(plot.subtitle = element_text(hjust = 0.5)) +
- theme(plot.caption = element_text(hjust = 0.5)) +
- geom_tile(color = "white") +
- labs(title = "Car Crashes by Time of Week in Nashville",
- subtitle = "Relative Frequency of Reported Crashes by Day, Hour",
- caption = paste("Data source: https://data.nashville.gov/browse?category=Public%20Safety\n",
- "Inspired by: https://www.reddit.com/r/dataisbeautiful/comments/i0a91n/car_crashes_by_time_and_day_chicago_oc/",
- sep = ""),
- x = "", y = "") +
- scale_fill_gradient(low = "white", high = "darkred") +
- scale_y_continuous(trans = "reverse",
- breaks = c(1, 2, 3, 4, 5, 6, 7),
- labels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) +
- scale_x_continuous(breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22),
- labels = c("12 am", "2 am", "4 am", "6 am", "8 am", "10 am", "12 pm", "2 pm ", "4 pm", "6 pm", "8 pm", "10 pm"))
- print(g_wrecks_hour_dow)
Add Comment
Please, Sign In to add comment