NYC Flights Assignment

Author

S Goon

Load Libraries

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.5.1     ✔ 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(nycflights23)
data("flights")

Selecting only the columns of data I need for my planned graph.

flights2 <- flights |>
select(time_hour, dep_delay, arr_delay) #The required dplyr command

Formatting the delay times.

flightslong <- flights2 |>
  pivot_longer(
    cols = 2:3, 
    names_to = "delay_times",
    values_to = "delay_length")
summary(flightslong)
   time_hour                      delay_times         delay_length     
 Min.   :2023-01-01 05:00:00.00   Length:870704      Min.   : -97.000  
 1st Qu.:2023-03-30 20:00:00.00   Class :character   1st Qu.: -11.000  
 Median :2023-06-27 08:00:00.00   Mode  :character   Median :  -4.000  
 Mean   :2023-06-29 10:02:22.39                      Mean   :   9.101  
 3rd Qu.:2023-09-27 11:00:00.00                      3rd Qu.:   9.000  
 Max.   :2023-12-31 23:00:00.00                      Max.   :1813.000  
                                                     NA's   :23272     

Creating, labeling and displaying the graph.

ggplot(flightslong) +
  geom_bar(aes(x = time_hour, y = delay_length, fill = delay_times),
           position = "dodge", stat = "identity") +
  ylim(-100, 1500) +
  scale_fill_discrete(name = "Type of Delay", labels = c("Departure Delays", "Arrival Delays")) +
  labs(y = "Length of the Delay (Minutes)",
       x = "Time of Departure",
       title = "NYC Delay Trends of 2023",
       caption = "Source: RITA, Bureau of transportation statistics, https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236")
Warning: Removed 23290 rows containing missing values or values outside the scale range
(`geom_bar()`).

Explanatory Paragraph

This graph shows the lengths of delays for scheduled flights that happened in NYC through the year 2023. The graph contains the delays of departures as well as arrivals, which are differentiated by their color on the graph. The highlight of the graph I would like to highlight is the negative delays, which indicated that some flights throughout the year departed or arrived before the scheduled time. Sadly, the outliers in the data, like some day-long delays, make it harder to see these early flights.