NYC_Flights

Using the NYC Flights data, create one visualization using any graph type covered so far in this course: bar graph, scatterplot, boxplot, histogram, treemap, heatmap, streamgraph, or alluvial diagram.

Requirements:

- Include at least one dplyr command, such as filter, arrange, summarize, group_by, select, or mutate.

- Label the x- and y-axes and include a caption naming the data source.

- Include a title.

- Use at least two different colors.

- Include a legend explaining what the colors represent.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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)

Cleaning the data

flight_summary <- flights |>
  mutate(
    Delay_Status = if_else(dep_delay > 15, "Delayed", "On Time")
  ) |>
  group_by(carrier, Delay_Status) |>
  summarize(
    Number_of_Flights = n(),
    .groups = "drop"
  )

Plot

ggplot(
  flight_summary,
  aes(
    x = carrier,
    y = Number_of_Flights,
    fill = Delay_Status
  )
) +
  geom_col(position = "dodge") +
  labs(
    title = "Number of Flights by Airline and Delay Status",
    x = "Airline Carrier",
    y = "Number of Flights",
    fill = "Flight Status",
    caption = "Data source: nycflights13 package"
  ) +
  scale_fill_manual(
    values = c(
      "Delayed" = "tomato",
      "On Time" = "steelblue"
    )
  ) +
  theme_minimal()