## picking variables from 19 ## loading Dplyr package library(nycflights23)library(dplyr)library(ggplot2)
## load fight Data set data(flights)
## new function to have 3 variables such as month , carrier and average of departure time for each airline ## Calculate average delays for each carrier in each month by calculating the mean of departure time ## grouping data with 2 variables : Month and carrier average_delays <- flights %>%group_by(month, carrier) %>%summarise(avg_dep_delay =mean(dep_delay, na.rm =TRUE), )
`summarise()` has grouped output by 'month'. You can override using the
`.groups` argument.
## checking the data set print(average_delays)
# A tibble: 165 × 3
# Groups: month [12]
month carrier avg_dep_delay
<int> <chr> <dbl>
1 1 9E 15.1
2 1 AA 15.0
3 1 AS 11.9
4 1 B6 16.1
5 1 DL 18.3
6 1 F9 35.6
7 1 G4 3.07
8 1 HA 11.7
9 1 MQ 23.6
10 1 NK 18.4
# ℹ 155 more rows
## Avarage_delays is 10 charectrs ( 10 carriers ) but 165 obs
## Creating a plot for Average delays and mapping Geom_line ## Grouping the all the 10 carriers together from 165 ## function color is to give each carrier one color p1<- average_delays |>ggplot( aes(x = month)) +geom_line(aes( x= month , y= avg_dep_delay , color = carrier , group = carrier), size=0.8) +scale_color_brewer(palette ="Paired")+## changing the x and y for the frame , and title labs(title ="Average Delays of Airlines by Month",x ="Month",y ="Average Delay (min)",color ="Airlines" ) +## Picking the theme theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
p1
Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Paired is 12
Returning the palette you asked for with that many colors
Warning: Removed 24 rows containing missing values or values outside the scale range
(`geom_line()`).
This visualization is based on three variables: all carriers (airlines), each month of the year, and a new variable, average delay, which represents the average departure delay time. This visualization will help the audience understand two important factors: which months of the year flights are most frequently delayed, and which airlines experience the most delays. According to the data, there are more delays during the summer than at any other time of year. These delays can be caused by a variety of factors, including weather, an increase in air traffic from more flights, and scheduling issues with flight crews. Travelers can use this information to choose the airlines that are most likely to have the least delays and, therefore, are the best choices for their trip.