library(tidyverse)
library(nycflights13)
library(viridis)NYC Flights Homework
flights_nona <- flights |>
filter(!is.na(distance) & !is.na(arr_delay))newflight <- flights_nona |>
group_by(carrier) |>
summarise(count = n(),
delay = mean(dep_delay),
)
newflight# A tibble: 16 × 3
carrier count delay
<chr> <int> <dbl>
1 9E 17294 16.4
2 AA 31947 8.57
3 AS 709 5.83
4 B6 54049 13.0
5 DL 47658 9.22
6 EV 51108 19.8
7 F9 681 20.2
8 FL 3175 18.6
9 HA 342 4.90
10 MQ 25037 10.4
11 OO 29 12.6
12 UA 57782 12.0
13 US 19831 3.74
14 VX 5116 12.8
15 WN 12044 17.7
16 YV 544 18.9
ggplot(data = newflight, aes(x = carrier, y = delay, fill = count)) +
geom_col(alpha = 0.7) +
scale_fill_viridis_c(option = "plasma") + # Using discrete color scale
labs(x = "Flight Carrier", y = "Average Departure Delay (Minutes)",
title = "Average Departure Delays of Carriers in NYC from nycflights13")The visualization I created was to show which airlines you can expect to have the highest departure delay. The graph shows the average departure delay in minutes of each carrier in the data set within that year. The colors legend shows how much flights were taken by each carrier that year. For example HA had an average flight delay of around 5 minutes with a very low amount of flights taken that year. I accomplished this by first grouping by the data set by the carriers, then creating a new variable which calculates the average departure delay. Then I decided that the best way graph to show the averages would be a bar graph. I created a bar graph using the data I created using ggplot and geom_col. I then used the color graph “plasma” from the viridis library to show how much flights each carrier had that year. One thing I would like to highlight about the graph is that although UA has had the highest amount of flights that year, the average departure delay time was around the middle compared to the rest of the carriers.