#install.packages("treemap")
#install.packages("RColorBrewer")
library(treemap)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(RColorBrewer)
library(nycflights13)
flights <- flights
flightsTm <- flights %>% 
 mutate(delayTF = if_else(dep_delay>30,1,0,0 ))  # creates delay flight time  set value to 1 if departure >30. I felt that individuals would get angry and unhappy after 30 minutes of delay.
flightsTm <- flightsTm %>% # group by carrier
  group_by(carrier)
flightsTm <- flightsTm %>% # summing up the delayed flight if > then 30 minutes
  summarise(countdelay = sum(delayTF),
    countflights = n())

The First graph represents the overall delay not accounting for the overall number of total flights

treemap(flightsTm, index="carrier", vSize="countflights", 
        vColor="countdelay", type="manual",    
        palette="PRGn",  title = "Total count delay after > 30 minutes flights per Delays By carrier",
        title.legend = "Counts of flights per delay")  

#install.packages("knitr")
library(knitr)
kable(flightsTm, 
      caption = "Table grouped by carrier counting delays > 30 minutes and total flight counts",
      digits = 2)  
Table grouped by carrier counting delays > 30 minutes and total flight counts
carrier countdelay countflights
9E 3257 18460
AA 3537 32729
AS 63 714
B6 8411 54635
DL 4982 48110
EV 11655 54173
F9 130 685
FL 547 3260
HA 16 342
MQ 3730 26397
OO 5 32
UA 7617 58665
US 1613 20536
VX 583 5162
WN 2030 12275
YV 115 601

Adjust for a total number of flights, after all the more flights the more delays are possible

flightsTm <- flightsTm %>%
  mutate(avg_flights_delay = countflights / countdelay) # averaging the number of flights before a delay > 30 minutes takes place.  

The second graph adjusting for a total number of flights

treemap(flightsTm, index="carrier", vSize="countflights", 
        vColor="avg_flights_delay", type="manual",   
        palette="PRGn",
        title = "Average flights numbers before delays takes place by carrier",
        title.legend = "Average flights number before delay takes place  ")

#install.packages("knitr")
library(knitr)
kable(flightsTm, 
      caption = "Table grouped by carrier counting delays > 30 minutes, total flight counts, and avrage flght count per delay",
      digits = 2)  # round values to 2 decimal places
Table grouped by carrier counting delays > 30 minutes, total flight counts, and avrage flght count per delay
carrier countdelay countflights avg_flights_delay
9E 3257 18460 5.67
AA 3537 32729 9.25
AS 63 714 11.33
B6 8411 54635 6.50
DL 4982 48110 9.66
EV 11655 54173 4.65
F9 130 685 5.27
FL 547 3260 5.96
HA 16 342 21.38
MQ 3730 26397 7.08
OO 5 32 6.40
UA 7617 58665 7.70
US 1613 20536 12.73
VX 583 5162 8.85
WN 2030 12275 6.05
YV 115 601 5.23

Note

The simple takeaway is that if the airline flies more flights, there will be more delays. Indeed, EVA airline (total flights 11,656) has a delay after an average of 4.65 flights, whereas Delta Airlines (total flights 4982), after an average of 9.66 flights, has a delay.

```