Nyc flights hw

Author

xutong zhang

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ 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)

Load the dataset and create a summary with average departure and arrival delays

flights_summary <- flights %>%
  select(carrier, dep_delay, arr_delay) %>%
  filter(!is.na(dep_delay), !is.na(arr_delay)) %>%
  group_by(carrier) %>%
  summarize(
    avg_dep_delay = mean(dep_delay, na.rm = TRUE),
    avg_arr_delay = mean(arr_delay, na.rm = TRUE)
  )

Create a bar plot with two different colors in the same column and a legend

ggplot(data = flights_summary, aes(x = carrier)) +
  geom_bar(aes(y = avg_dep_delay, fill = "Average Departure Delay"), stat = "identity", position = "dodge") +
  geom_bar(aes(y = avg_arr_delay, fill = "Average Arrival Delay"), stat = "identity", position = "dodge") +
  labs(
    x = "Carrier",
    y = "Average Delay (minutes)",
    title = "Average Departure and Arrival Delays by Carrier",
    fill = "Delay Type"
  ) +
  scale_fill_manual(values = c("Average Departure Delay" = "blue", "Average Arrival Delay" = "red")) +
  theme_minimal() +
  theme(legend.position = "top")

The presented bar plot offers a visual representation of the average departure delay and average arrival delay for various airline carriers operating at New York airports. Each carrier is denoted on the x-axis, while the y-axis indicates the average delay in minutes. The plot employs two distinct colors, blue for “Average Departure Delay” and red for “Average Arrival Delay,” to differentiate between these two essential metrics. This visualization highlights the significant variability in delay patterns among carriers.

The one aspect of the plot that I would like to highlight is the significant variability in arrival delays among different airline carriers, even when their departure delays are relatively low. This observation underscores the importance of not only focusing on departure punctuality but also considering the efficiency of arrival schedules for a comprehensive assessment of airline performance.