NYC Flights Homework

Author

Merveille Kuendzong

Published

February 25, 2024

library(tidyverse)
Warning: package 'dplyr' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ 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)
Warning: package 'nycflights13' was built under R version 4.3.2
data(flights)
morning_flights_dec <- flights |>
  filter(dep_time >= 600 & dep_time < 1200 & month == 12 & !is.na(distance))

plot <- morning_flights_dec |>
  ggplot() +
  stat_summary(
    aes(x = carrier, y = distance, fill = origin),
    fun = "mean",
    geom = "bar",
    position = "dodge") +
  labs(fill = "Origin Airport Code",
       x = "Airline Carrier Code",
       y = "Mean Distance of flights (in miles)",
       title = "Mean Distance of Morning Flights in December by Carrier and Origin",
       caption = "Data: nycflights13 package")
plot

This barplot visualizes the mean distance of morning flights in December 2013 by airline carrier and origin airport. The data is first filtered to include only morning flights in December with non-missing distance values. The ggplot2 code employs stat_summary to compute and visualize the mean distance for each carrier and origin airport combination as bars, with different colors indicating different airports of origin. The x-axis represents airline carrier codes, the y-axis indicates the mean distance in miles, and the legend denotes the origin airport codes.

Notably, the plot reveals that, for certain carrier codes, all flights originate from a single airport. For instance, flights by carrier AS exclusively depart from EWR, while those of FL, F9, and YV carriers all originate from LGA, and carrier HA’s flights exclusively depart from JFK.”