• 1 Lab 5 - Tables & Fonts
  • 2 List of packages required for this analysis
    • 2.1 Tables
    • 2.2 Worst departures from PDX per month
    • 2.3 Worst Departures from SEA per month
    • 2.4 Best Departures per month from PDX
    • 2.5 Best departures per month from SEA
    • 2.6 The airplines, which improved on departure time:
    • 2.7 Which specific aircraft accumulate the most flight time? (I used top 10)
    • 2.8 Which specific aircraft log the most distance? (I used top 10)
    • 2.9 Fonts
    • 2.10 Fonts

1 Lab 5 - Tables & Fonts

install.packages("devtools")
Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/3.6'
(as 'lib' is unspecified)
devtools::install_github("ismayc/pnwflights14")
Skipping install of 'pnwflights14' from a github remote, the SHA1 (0a122d90) has not changed since last install.
  Use `force = TRUE` to force installation

2 List of packages required for this analysis

pkg <- c("dplyr", "knitr", "devtools", "DT", "xtable")

# Check if packages are not installed and assign the
# names of the packages not installed to the variable new.pkg
new.pkg <- pkg[!(pkg %in% installed.packages())]

# If there are any packages in the list that aren't installed,
# install them
if (length(new.pkg)) {
  install.packages(new.pkg, repos = "http://cran.rstudio.com")
}

# Load the packages into R
library(dplyr)
library(knitr)
library(DT)
library(xtable)
library(tidyverse)
library(skimr)
library(janitor)
library(ggridges)

# Install Chester's pnwflights14 package (if not already)
if (!require(pnwflights14)){
  library(devtools)
  devtools::install_github("ismayc/pnwflights14")
  }
library(pnwflights14)

2.1 Tables

Note that the pnwflights14 package includes several other data sets that may be of use (mappings from two-letter airline codes to airline names, weather conditions, etc.).

2.1.1 Question 1

Which airlines had the best and worst track records of on-time departures in each month? Is it different between PDX and SEA?

2.2 Worst departures from PDX per month

depart_late_PDX <- flights %>%
  select(year, month, origin, carrier, dep_delay, arr_delay, dest) %>%
  filter((origin=="PDX" & dep_delay>0)) %>%
  group_by(origin, year, month, carrier) %>%
  summarize(max_delay=max(dep_delay, na.rm=TRUE)) %>%
  arrange(desc(max_delay),origin, year, month, carrier) %>%
  distinct(month, .keep_all = TRUE)
`summarise()` has grouped output by 'origin', 'year', 'month'. You can override using the `.groups` argument.
knitr::kable(depart_late_PDX)
origin year month carrier max_delay
PDX 2014 3 AA 1553
PDX 2014 12 AA 798
PDX 2014 4 AA 782
PDX 2014 5 AA 695
PDX 2014 7 DL 648
PDX 2014 6 F9 590
PDX 2014 8 UA 486
PDX 2014 10 UA 428
PDX 2014 9 HA 417
PDX 2014 11 AA 402
PDX 2014 2 DL 380
PDX 2014 1 US 346

2.3 Worst Departures from SEA per month

depart_late_SEA <- flights %>%
  select(year, month, origin, carrier, dep_delay, arr_delay, dest) %>%
  filter((origin=="SEA" & dep_delay>0)) %>%
  group_by(origin, year, month, carrier) %>%
  summarize(max_delay=max(dep_delay, na.rm=TRUE)) %>%
  arrange(desc(max_delay),origin, year, month, carrier) %>%
  distinct(month, .keep_all = TRUE)
`summarise()` has grouped output by 'origin', 'year', 'month'. You can override using the `.groups` argument.
knitr::kable(depart_late_SEA)
origin year month carrier max_delay
SEA 2014 5 AA 1449
SEA 2014 8 DL 886
SEA 2014 1 AS 866
SEA 2014 7 F9 815
SEA 2014 2 DL 739
SEA 2014 12 AA 733
SEA 2014 4 AA 713
SEA 2014 10 AA 713
SEA 2014 9 DL 452
SEA 2014 11 HA 397
SEA 2014 3 AA 385
SEA 2014 6 WN 377

2.4 Best Departures per month from PDX

depart_best_PDX <- flights %>%
  select(year, month, origin, carrier, dep_delay, arr_delay, dest) %>%
  filter(origin=="PDX") %>%
  group_by(origin, year, month, carrier) %>%
  summarize(min_delay=min(dep_delay, na.rm=TRUE)) %>%
  arrange(min_delay,origin, year, month, carrier) %>%
  distinct(month, .keep_all = TRUE)
`summarise()` has grouped output by 'origin', 'year', 'month'. You can override using the `.groups` argument.
knitr::kable(depart_best_PDX)
origin year month carrier min_delay
PDX 2014 4 AS -25
PDX 2014 1 AS -23
PDX 2014 11 AS -21
PDX 2014 6 AS -20
PDX 2014 10 F9 -20
PDX 2014 12 AS -20
PDX 2014 2 AS -19
PDX 2014 9 F9 -19
PDX 2014 5 AS -18
PDX 2014 3 AS -17
PDX 2014 8 AS -17
PDX 2014 7 AA -16

2.5 Best departures per month from SEA

depart_best_SEA <- flights %>%
  select(year, month, origin, carrier, dep_delay, arr_delay, dest) %>%
  filter(origin=="SEA") %>%
  group_by(origin, year, month, carrier) %>%
  summarize(min_delay=min(dep_delay, na.rm=TRUE)) %>%
  arrange(min_delay, origin, year, month, carrier) %>%
  distinct(month, .keep_all = TRUE)
`summarise()` has grouped output by 'origin', 'year', 'month'. You can override using the `.groups` argument.
knitr::kable(depart_best_SEA)
origin year month carrier min_delay
SEA 2014 2 OO -37
SEA 2014 8 US -26
SEA 2014 1 VX -21
SEA 2014 5 AS -21
SEA 2014 10 VX -20
SEA 2014 3 OO -19
SEA 2014 4 B6 -19
SEA 2014 9 AS -19
SEA 2014 11 AS -19
SEA 2014 12 AS -18
SEA 2014 7 B6 -17
SEA 2014 6 AS -16

2.5.1 Question 2

Which airlines improved the most in terms of on-time departures over time, and on which routes? Which airlines got worse?

2.6 The airplines, which improved on departure time:

improves <- flights %>%
  select(year, month, origin, carrier, dep_delay) %>%
  filter(origin=="SEA") %>%
  group_by(carrier, year) %>%
  mutate(improvement=max(dep_delay, na.rm=TRUE)-min(dep_delay, na.rm=TRUE), max=max(dep_delay, na.rm=TRUE), min=min(dep_delay, na.rm=TRUE)) %>%
  arrange(desc(improvement), carrier, year, max, min) %>%
  distinct(carrier, .keep_all = TRUE)

knitr::kable(improves)
year month origin carrier dep_delay improvement max min
2014 1 SEA AA -3 1466 1449 -17
2014 1 SEA DL 82 905 886 -19
2014 1 SEA HA 39 895 878 -17
2014 1 SEA AS 44 887 866 -21
2014 1 SEA F9 -3 832 815 -17
2014 1 SEA US -6 737 711 -26
2014 1 SEA OO -2 714 677 -37
2014 1 SEA UA 227 599 580 -19
2014 1 SEA WN -3 577 567 -10
2014 1 SEA B6 -4 385 365 -20
2014 1 SEA VX -6 352 331 -21

2.6.1 Question 3

2.7 Which specific aircraft accumulate the most flight time? (I used top 10)

busy_plane <- flights %>%
  select(tailnum, carrier, air_time) %>%
  group_by(tailnum, carrier) %>%
  summarise(tot_time=sum(air_time, na.rm=TRUE)) %>%
  arrange(desc(tot_time))
`summarise()` has grouped output by 'tailnum'. You can override using the `.groups` argument.
head(busy_plane, n=10)
tailnum carrier tot_time
N435AS AS 103172
N440AS AS 101859
N423AS AS 100384
N409AS AS 99199
N431AS AS 98696
N442AS AS 98246
N419AS AS 97468
N408AS AS 96504
N407AS AS 96001
N433AS AS 95458

2.8 Which specific aircraft log the most distance? (I used top 10)

tot_dist_plane <- flights %>%
  select(tailnum, carrier, distance) %>%
  group_by(tailnum, carrier) %>%
  summarise(tot_dist=sum(distance, na.rm=TRUE)) %>%
  arrange(desc(tot_dist))
`summarise()` has grouped output by 'tailnum'. You can override using the `.groups` argument.
head(tot_dist_plane, n=10)
tailnum carrier tot_dist
N435AS AS 828824
N440AS AS 819745
N423AS AS 811225
N442AS AS 802564
N409AS AS 798398
N431AS AS 794324
N408AS AS 781197
N419AS AS 780324
N433AS AS 773300
N407AS AS 773154

2.9 Fonts

Error in install.packages : Updating loaded packages
Exiting.

fonts()

2.9.1 Serif

library(showtext)
font_add_google("Noto Serif")

library(ggplot2)
text = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
    theme(axis.title = element_blank(), axis.ticks = element_blank(),
          axis.text = element_blank()) +
    annotate("text", 1, 0.9, label = 'This is a standard font.',
             family = "Noto Serif", fontface = "italic", size = 12)
text

2.9.2 Sanserif

font_add_google("Raleway")

text = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
    theme(axis.title = element_blank(), axis.ticks = element_blank(),
          axis.text = element_blank()) +
    annotate("text", 1, 0.9, label = 'This is a bold, square font.',
             family = "Raleway", fontface = "bold", size = 12)
text

2.9.3 Display

font_add_google("Red Hat Display")
text = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
    theme(axis.title = element_blank(), axis.ticks = element_blank(),
          axis.text = element_blank()) +
    annotate("text", 1, 0.9, label = 'This is a casual font.',
             family = "Red Hat Display", fontface = "italic", size = 12)
text

2.10 Fonts