Heatmap of Flight Traffic by Month and Day of Week

Author

Rebecca Jipdjio

Published

February 22, 2024

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nycflights13)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout

Filtering out missing values

flights <- flights %>%
  filter(if_any(everything(), ~!is.na(.)))

Creating an ordered factor column for the day of the week

flights <- flights %>%
  mutate(day_of_week = ordered(weekdays(as.Date(paste(year, month, day, sep = "-"))),
                               levels = c( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")))

Grouping by month & day (calculating flight counts)

flight_traffic <- flights %>%
  group_by(month, day_of_week) %>%
  summarise(num_flights = n(), .groups = 'drop') %>%
  mutate(month = factor(month.abb[month], levels = month.abb))

Creating the heatmap directly with ggplotly

heatmap_plot <- ggplot(flight_traffic, aes(x = month, y = day_of_week, fill = num_flights, text = paste("Month:", month, "<br>Day of Week:", day_of_week, "<br>Flights:", num_flights))) +
  geom_tile() +
  scale_fill_gradient(low = "lightblue", high = "darkblue", name = "Number of Flights") +
  labs(title = "Flight Traffic by Month and Day of Week",
       x = "Month", y = "Day of Week") +
  theme_minimal() +
  scale_x_discrete(labels = month.abb)
heatmap_interactive <- ggplotly(heatmap_plot, tooltip = "text")

Displaying the interactive heatmap

heatmap_interactive

Description:

I’ve developed an interactive heatmap visualization to illustrate flight traffic based on both month and day of the week. Each rectangle within the heatmap corresponds to a specific combination of month and day, with its size reflecting the volume of flights on that day. The color intensity of each rectangle denotes the level of flight activity, with darker hues indicating higher traffic. One noteworthy aspect of this visualization is its ability to highlight the fluctuations in flight traffic across various months and days of the week, aiding in the identification of patterns and trends in air travel. Additionally, I’ve enhanced user interaction by converting the initial static ggplot visualization into an interactive plotly version. This upgrade allows users to hover over individual tiles to access detailed information such as the month, day of the week, and number of flights, creating a more immersive and comprehensive exploration of flight traffic patterns.