Traffic Through Time: Weekday Routines, Weekend Variations
Exploring traffic dynamics on the A64 road, this analysis reveals stark contrasts between weekday rush hour plateaus and weekend midday peaks, highlighting how traffic volume shifts with daily routines and leisure patterns.
#TidyTuesday
Time Series
Author
Aditya Dahiya
Published
November 25, 2024
About the Data
The dataset for this week explores National Highways Traffic Flow, focusing on vehicle speed and size information collected in May 2021. This data originates from the National Highways API, which monitors traffic on motorways and major A roads across England. The dataset specifically covers four road sensors along the A64 road, recording variables such as vehicle size categories, speed ranges, and average speeds. Key questions include whether vehicle speeds vary by day or time, the time of day large vehicles are most active, and whether smaller vehicles tend to travel faster. Curated by Nicola Rennie, this dataset provides an opportunity to practice data tidying and visualization. For data access, you can use the tidytuesdayRpackage or directly read the data from GitHub.
Summary of Findings:
The traffic volume patterns on the A64 road exhibit distinct differences between weekdays and weekends. On weekdays, traffic peaks between 8–9 AM, coinciding with morning rush hours, and then plateaus at a steady level until the evening (around 7 PM) before declining. In contrast, weekends show a different pattern, with traffic gradually rising to a peak during midday (around noon) and then declining, without the characteristic morning and evening rush hour peaks. Additionally, the overall traffic volume is notably lower on weekends compared to weekdays. This consistent pattern is observed across all four sensor locations, indicating a uniform trend in traffic behavior along the A64 road.
How I made this graphic?
Loading required libraries, data import & creating custom functions.
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plotsa64_traffic <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-12-03/A64_traffic.csv')
Visualization Parameters
Code
# Font for titlesfont_add_google("Offside",family ="title_font") # Font for the captionfont_add_google("Saira Extra Condensed",family ="caption_font") # Font for plot textfont_add_google("Rajdhani",family ="body_font") showtext_auto()mypal <-c("#418FDEFF", "#003DA5FF", "#D50032FF")# A base Colourbg_col <-"#FFFFFF"seecolor::print_color(bg_col)# Colour for highlighted texttext_hil <- mypal[2]seecolor::print_color(text_hil)# Colour for the texttext_col <-darken(mypal[2], 0.4)seecolor::print_color(text_col)# Define Base Text Sizebts <-90# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""xtwitter_username <-"@adityadahiyaias"social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username} </span>")social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")# Add text to plot--------------------------------------------------------------plot_title <-"Traffic Flow on the A64:\nWeekdays' Hustle, Weekends' Ease"plot_subtitle <-" Traffic patterns on the A64 road vary significantly between weekdays and weekends, with weekday traffic peaking during morning rush hours and plateauing through the day, while weekend traffic peaks around midday and shows lower overall volumes. This trend is consistent across all four sensor locations."plot_caption <-paste0("**Data:** National Highways API, Nicola Rennie", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)
Exploratory Data Analysis and Wrangling
Code
# a64_traffic |> # summarytools::dfSummary() |> # summarytools::view()temp_names <- a64_traffic$Name |>unique()rawdf <- a64_traffic |> janitor::clean_names() |>mutate(report_day =wday(report_date, label = T, abbr = F),weekend =if_else( report_day %in%c("Saturday", "Sunday"),"Weekend","Weekdays" ) ) |>mutate(name =case_when( name == temp_names[1] ~"Norton-on-Derwent (east)", name == temp_names[2] ~"Between B1249 and A1039", name == temp_names[3] ~"Near York (north)", name == temp_names[4] ~"Between B1261 and Filey", ) ) |>select(total_volume, time_period_ending, report_date, weekend, report_day, name) |>mutate(name =str_wrap(name, 20))
# Saving a thumbnaillibrary(magick)# Saving a thumbnail for the webpageimage_read(here::here("data_vizs", "tidy_nh_uk_traffic.png")) |>image_resize(geometry ="x400") |>image_write( here::here("data_vizs", "thumbnails", "tidy_nh_uk_traffic.png" ) )
Session Info
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Compiling Plotssessioninfo::session_info()$packages |>as_tibble() |>select(package, version = loadedversion, date, source) |>arrange(package) |> janitor::clean_names(case ="title" ) |> gt::gt() |> gt::opt_interactive(use_search =TRUE ) |> gtExtras::gt_theme_espn()