1 + 1[1] 2
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
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(nycflights23)
data(flights)
data(airlines)flights_nona <- flights |>
filter(!is.na(dep_delay))by_airline <- flights_nona |>
group_by(carrier) |>
summarise(
avg_dep_delay = mean(dep_delay),
count = n()
)
print(by_airline) # A tibble: 14 × 3
carrier avg_dep_delay count
<chr> <dbl> <int>
1 9E 7.44 52380
2 AA 14.2 39940
3 AS 12.0 7774
4 B6 23.8 64622
5 DL 15.1 60616
6 F9 35.7 1218
7 G4 3.98 667
8 HA 22.9 364
9 MQ 10.5 354
10 NK 18.2 14827
11 OO 19.8 6214
12 UA 17.6 77810
13 WN 16.1 12105
14 YX 4.21 85723
by_airline <- by_airline |>
left_join(airlines, by = "carrier")
print(by_airline)# A tibble: 14 × 4
carrier avg_dep_delay count name
<chr> <dbl> <int> <chr>
1 9E 7.44 52380 Endeavor Air Inc.
2 AA 14.2 39940 American Airlines Inc.
3 AS 12.0 7774 Alaska Airlines Inc.
4 B6 23.8 64622 JetBlue Airways
5 DL 15.1 60616 Delta Air Lines Inc.
6 F9 35.7 1218 Frontier Airlines Inc.
7 G4 3.98 667 Allegiant Air
8 HA 22.9 364 Hawaiian Airlines Inc.
9 MQ 10.5 354 Envoy Air
10 NK 18.2 14827 Spirit Air Lines
11 OO 19.8 6214 SkyWest Airlines Inc.
12 UA 17.6 77810 United Air Lines Inc.
13 WN 16.1 12105 Southwest Airlines Co.
14 YX 4.21 85723 Republic Airline
ggplot(by_airline, aes(x = name, y = avg_dep_delay, fill = name)) +
geom_bar(stat = "identity") +
labs(
title = "Average Departure Delay by Airline",
x = "Airline",
y = "Average Departure Delay (Minutes)",
fill = "Airline",
caption = "Source: NYCFlights23"
) +
theme_minimal() +
coord_flip()I decided to use a bar graph even though we used the box chart in class because personally it is easier for me to understand this information in this format. Because of that, we can immediately see that Frontier Airlines has by far the highest average delay out of all 14 airlines at around 36 minutes. Then we have JetBlue and Hawaiian Airlines with pretty high average delays at around 23 minutes, but nothing as crazy as Frontier. But if we take a look at Allegiant Air and Republic Airline, they have the lowest averages at just around 4 minutes, which I think is very impressive. Republic Airline especially stands out because it also has a huge roster of flights, with over 85,000 flights in the dataset, while still having one of the lowest average delays. If you ask me, I think this graph does an incredible job at showing this information in a nice and easy to understand way.