New York City Flights Homework

Author

Cody Paulay-Simmons

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.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── 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")
data(airlines)

Average Speed by Designation

Create speed (in miles per hour, mph) and average by destination

flight_speeds <- flights |>
  filter(!is.na(air_time)) |>
  mutate(speed = distance / (air_time / 60)) |>
  group_by(dest) |>
  summarize(avg_speed = mean(speed),
  total_flights = n()) |>
  filter(total_flights > 10000)

Plot the results

ggplot(flight_speeds, aes(x = reorder(dest, avg_speed), y = avg_speed, fill = avg_speed)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  labs(
    title = "Average Flight Speed by Destination (NYC, 2023)",
    x = "Destination Airport Code",
    y = "Average Speed (mph)",
    caption = "Source: nycflights23 package"
  )

Summary

This chart shows the average flight speed to different destinations from NYC in 2013. I calculated speed by dividing the distance by air time and then took the average for each destination. It tells us that the further the destination is, the faster the plane goes. I filtered the data to only include destinations with more than 10,000 flights. I did this because when I included all destinations, the chart became super crammed with airport codes, and it was hard to read. I wanted to include all of them, but I wasn’t sure how to fix the spacing. Maybe if I could make the chart interactive, I could include everything and just show the details, exact average speed and full airport name, when you hover over each bar with the mouse. That way it wouldn’t look messy, and you could still see all the info one at a time.