install.packages("nycflights13")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/nycflights13_1.0.2.zip'
Content type 'application/zip' length 4510300 bytes (4.3 MB)
downloaded 4.3 MB
package ‘nycflights13’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kris.sutton\AppData\Local\Temp\RtmpKKqsAH\downloaded_packages
install.packages("psych")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/psych_2.1.3.zip'
Content type 'application/zip' length 4105483 bytes (3.9 MB)
downloaded 3.9 MB
package ‘psych’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kris.sutton\AppData\Local\Temp\RtmpKKqsAH\downloaded_packages
## load libraries
library(tidyverse)
library(nycflights13)
library(psych)
library(treemap)
Error in library(treemap) : there is no package called ‘treemap’
## putting flights in global en
flights
view(flights)
## install treemap package
install.packages("treemap")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/treemap_2.4-2.zip'
Content type 'application/zip' length 288633 bytes (281 KB)
downloaded 281 KB
package ‘treemap’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\kris.sutton\AppData\Local\Temp\RtmpKKqsAH\downloaded_packages
library(treemap)
Warning: package ‘treemap’ was built under R version 4.0.5
Registered S3 method overwritten by 'data.table':
method from
print.data.table
treemap(flights, index="carrier", vSize="dep_delay",
vColor="arr_delay", type="value",
palette="GnBu")
NA
NA
##delay_flights <- group_by(flights, carrier, drop = group_by_drop_default(flights))
## arranging by carrier
flights2 <- select(flights, carrier, dep_delay, arr_delay, air_time)
by_carrier <- arrange(flights2, desc(carrier))
by_carrier <- by_carrier %>%
drop_na(dep_delay, arr_delay, air_time)
head(by_carrier)
NA
delay_sum <- by_carrier %>%
group_by(carrier) %>%
summarize(tot_dep_delay = sum(dep_delay), tot_arr_delay = sum(arr_delay), tot_air_time = sum(air_time)) %>%
arrange(desc(carrier))
head(delay_sum)
NA
#creating total delay column: departure delay + arrival delay
delay_sum <- delay_sum %>%
mutate(total_min_delay = tot_dep_delay + tot_arr_delay)
#creating efficiency column: total delay / total air time
delay_sum <- delay_sum %>%
mutate(inefficiency = total_min_delay / tot_air_time)
##filter to remove negative values from inefficiency column
delay_sum <- delay_sum %>%
filter(inefficiency > 0)
head(delay_sum)
NA
NA
## create treemap with data
treemap(delay_sum, index="carrier", vSize="perc_delays_airtime",
vColor="total_min_delay", type="value",
title = "Airline Inefficiency",
title.legend = "Total Minutes Delayed",
palette="OrRd")
For my homework using the “nycflights13” data frame, I decide to create a tree map that shows each airline’s total delay time (departure and arrival delays) as a percentage of total air. The resulting percentage is defined as “inefficiency” for this treemap.
The highlight I wish to point to is initially the intent was to compare total airtime to total delay time, but ultimately wanted to correct for economy of scale bias, and pivoted to a percentage output for my index.
I am including a legend for the airline codes below. For next time, how can I change the codes to the names within the data?
YV – Mesa WN – Southwest VX – VirginAm US – USAirways UA – United OO – Skywest MQ – Envoy HA – Hawaiian FL – AirTran F9 – Frontier EV – ExpressJet DL – Delta B6 – JetBlue AS – AlaskAirlines AA – American 9E - Endeavor