NYC Flights Assignment

Loading the Data

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)
data(flights)

Filtering out NA values

flights_nona <- flights |>
  filter(!is.na(dep_delay) & !is.na(arr_delay))

Grouping the number of delays by carrier

dep_delays <- flights_nona|>
  group_by(carrier)|>
  summarise(Departure = sum(dep_delay >=0))
arr_delay <- flights_nona|>
  group_by(carrier)|>
  summarise(Arrival = sum(arr_delay >=0))

Filtering the data into the top 5 values

by_carrier <- left_join(dep_delays, arr_delay, by = "carrier")

#Source: ChatGPT
by_carrier_top5 <- by_carrier|>
  slice_max(order_by = Departure+Arrival, n=5)
by_carrier_long <- by_carrier_top5 |>
  pivot_longer(cols = c(Departure, Arrival), names_to = "Delay Type", values_to = "Number of Delays")

Creating the chart

ggplot(by_carrier_long, aes(x = `Number of Delays`, y = reorder(carrier, `Number of Delays`), fill = `Delay Type`)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("Departure" = "orange", "Arrival" = "red")) +  # Manually set colors
  labs(fill = "Delay Type",
       y = "Carrier",
       x = "Number of Delays",
       title = "Number of Delays by Carrier in 2013",
       caption = "Source: RITA, Bureau of transportation statistics https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236")

  1. My visualization is a grouped bar chart that displays the top 5 carriers with the most flight delays in 2013. The types of delays experienced are separated based on color, which is displayed in the legend to the right. One aspect that I wanted to highlight was how deceptive this visualization could be. While the information within the data set hasn’t been manipulated or altered, it also does not take other important variables into account. This can create misleading information for any readers.