Week 5 Practice

Author

Emma Wang

library(nycflights23)
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(viridisLite)
library(ggalluvial)
library(alluvial)
data(flights)
data(airlines)
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>
head(airlines)
# A tibble: 6 × 2
  carrier name                  
  <chr>   <chr>                 
1 9E      Endeavor Air Inc.     
2 AA      American Airlines Inc.
3 AS      Alaska Airlines Inc.  
4 B6      JetBlue Airways       
5 DL      Delta Air Lines Inc.  
6 F9      Frontier Airlines Inc.
flights_nona <- flights |>
  filter(!is.na(distance) & !is.na(arr_delay) & !is.na(dep_delay))  
flights_nona
# A tibble: 422,818 × 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
 7  2023     1     1      520            510        10      948            949
 8  2023     1     1      524            530        -6      645            710
 9  2023     1     1      537            520        17      926            818
10  2023     1     1      547            545         2      845            852
# ℹ 422,808 more rows
# ℹ 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>
by_dest <- flights_nona |>
  group_by(dest) |>  
  summarise(count = n(),   
            avg_dist = mean(distance), 
            avg_arr_delay = mean(arr_delay),  
            avg_dep_delay = mean(dep_delay), 
            .groups = "drop") |>  
  arrange(avg_arr_delay) |>
  filter(avg_dist < 3000)
head(by_dest)
# A tibble: 6 × 5
  dest  count avg_dist avg_arr_delay avg_dep_delay
  <chr> <int>    <dbl>         <dbl>         <dbl>
1 PNS      71    1030         -10.6         -1.24 
2 HHH     461     695.         -9.95         1.38 
3 HDN      27    1728          -9.93         8.78 
4 VPS     107     988          -9.41         2.62 
5 AVP     140      93          -8.53        -0.957
6 GSO    2857     456.         -7.77         3.81 
ggplot(by_dest, aes(avg_dist, avg_arr_delay)) +
  geom_point(aes(size = count), alpha = .3) +
  geom_smooth(se = FALSE) + # remove the error band
  scale_size_area() + #scales the size aesthetic based on the area of the plot element
  #theme_bw() +  # theme black white
  theme_minimal()+
  labs(x = "Average Flight Distance (miles)",
       y = "Average Arrival Delay (minutes)",
       size = "Number of Flights \n Per Destination",
       caption = "Source: FAA Aircraft registry",
       title = "Average Distance and Average Arrival Delays from Flights from NY")
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

by_dest_matrix <- data.matrix(by_dest[, -1])  
row.names(by_dest_matrix) <- by_dest$dest     
by_dest_heatmap <- heatmap(by_dest_matrix, 
                           Rowv=NA, 
                           Colv=NA, 
                           col = viridis(25), 
                           cexCol = .4,  
                           scale="column", 
                           xlab = "",
                           ylab = "",
                           main = "Heatmap of Flight Destinations from NY Airports")

data(Refugees)
ggalluv <- Refugees |>
  ggplot(aes(x = year, y = refugees, alluvium = country)) + 
  theme_bw() +
  geom_alluvium(aes(fill = country), 
                color = "white",
                width = .1, 
                alpha = .8,
                decreasing = FALSE) +
  scale_fill_brewer(palette = "Spectral") + 
  scale_x_continuous(lim = c(2002, 2013)) +
  labs(title = "UNHCR-Recognised Refugees Top 10 Countries\n (2003-2013)",
       y = "Number of Refugees", 
       fill = "Country",
       caption = "Source: United Nations High Commissioner for Refugees (UNHCR)")
ggalluv

options(scipen = 999)
ggalluv