NYC Flights Homework

Author

G. Balasanov

Loading Libraries

# Loading libraries
library(tidyverse) #I don't load dplyr and ggplot2, since tidyverse loads everything
library(nycflights23)

# viewing the 'fligts' dataset
data(flights)

Creating data visualization: bar graph (grouped)

# Doing calculations and extracting necessary data, grouping

# Average departure delay for each month and each NYC airport
delay_by_month <- flights %>%
  
# Replacing the 3-letter airport codes with full airport names
mutate(origin_name = case_when(
  origin == "EWR" ~ "Newark Liberty International",
  origin == "JFK" ~ "John F. Kennedy International",
  origin == "LGA" ~ "LaGuardia"
  )) %>%
  
# Group by month and airport
group_by(month, origin_name) %>%
  
# Summarise and skip cancelled flights, which have no delay value
summarise(avg_dep_delay = mean(dep_delay, na.rm = TRUE), .groups = "drop") %>%
  
# Turn month numbers into month names, in calendar order
mutate(month_name = factor(month, levels = 1:12, labels = month.abb))
ggplot(delay_by_month, aes(x = month_name, y = avg_dep_delay, fill = origin_name)) +
  geom_col(position = "dodge") +
  labs(
    title = "Average Departure Delay by Month at New York City Airports, 2023",
    x = "Month",
    y = "Average departure delay (minutes)",
    fill = "Departure airport",
    caption = "Source: nycflights23 R package (flights dataset)"
  ) +
  theme(legend.position = "bottom")

Finding months with the highest and lowest delays

# Here I'm fiding months with the highest and lowest delays
delay_by_month %>% arrange(desc(avg_dep_delay)) %>% head(3)
# A tibble: 3 × 4
  month origin_name                   avg_dep_delay month_name
  <int> <chr>                                 <dbl> <fct>     
1     7 Newark Liberty International           36.2 Jul       
2     6 Newark Liberty International           33.4 Jun       
3     7 John F. Kennedy International          33.1 Jul       
delay_by_month %>% arrange(avg_dep_delay) %>% head(3)
# A tibble: 3 × 4
  month origin_name                  avg_dep_delay month_name
  <int> <chr>                                <dbl> <fct>     
1    11 LaGuardia                             2.66 Nov       
2    10 LaGuardia                             3.75 Oct       
3    10 Newark Liberty International          3.85 Oct       

Visualization Description

This visualization is a grouped bar graph of the average departure delay, in minutes, for each month of 2023 at the three New York City airports: Newark Liberty International, John F. Kennedy International, and LaGuardia.

Each month has three bars, one for each airport, and the legend at the bottom shows which color matches which airport. To build it, I used dplyr to replace the airport codes with their full names, group the flights by month and airport, and calculate the average delay.

The part I want to highlight is the summer. July has the tallest bars, and Newark Liberty International had the worst average in July, with flights leaving about 36.2 minutes late. June at Newark (33.4 minutes) and July at John F. Kennedy International (33.1 minutes) were close behind. In contrast, the fall had the shortest bars. LaGuardia in November had an average delay of only 2.7 minutes, and October was also low at LaGuardia (3.8 minutes) and Newark (3.9 minutes).

All of the above suggests, that delays are much worse in the summer than in the fall, probably because of summer storms and busy travel. The only limitation is that an average can be pulled up by a few very long delays, so it does not show what a typical flight experiences.