Assignment 4

Author

Emma Wang

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(dplyr)
library(nycflights23)
library(ggplot2)
data(flights)
head(flights)
# A tibble: 6 × 19
   year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
  <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
1  2023     1     1        1           2038       203      328              3
2  2023     1     1       18           2300        78      228            135
3  2023     1     1       31           2344        47      500            426
4  2023     1     1       33           2140       173      238           2352
5  2023     1     1       36           2048       228      223           2252
6  2023     1     1      503            500         3      808            815
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
#   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#   hour <dbl>, minute <dbl>, time_hour <dttm>
flights_origin = flights |> 
  select(origin) |> 
  group_by(origin) |> 
  count() |> 
  arrange(desc(n))
head(flights_origin)
# A tibble: 3 × 2
# Groups:   origin [3]
  origin      n
  <chr>   <int>
1 LGA    163726
2 EWR    138578
3 JFK    133048
flights_month = flights |> 
  select(origin, month) |> 
  group_by(origin, month) |> 
  count() |> 
  arrange(desc(n))
head(flights_month)
# A tibble: 6 × 3
# Groups:   origin, month [6]
  origin month     n
  <chr>  <int> <int>
1 LGA        3 14763
2 LGA        5 14517
3 LGA        8 14074
4 LGA       10 13861
5 LGA        4 13816
6 LGA        6 13568
view(flights_month)
flights_month |> ggplot(aes(month, origin, fill = n)) + 
    geom_tile(color = "black") + 
  scale_fill_gradient(low = "blue", high = "black") +
  labs(title = "New York Aiport's Monthly Flight Traffic", x = "Month", y = "Airport") +
  labs(fill = "Flight Count")

The heatmap above shows each New York airport in the dataset and its monthly traffic levels.  Dark blue symbolizes lower traffic levels, while black indicates higher flight traffic levels. Straight away, we can see that LGA (LaGuardia) has the highest flight traffic year-round out of the three airports. Visualizing the data in this manner allows us to see which airports generate higher flight volumes throughout the year. The legend on the side provides a color guide for the heatmap, along with a numerical representation of the color gradient. One thing to highlight is that the months are shown as decimal values, allowing us to see flight volumes from different periods within each month.