library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.0 v dplyr 1.0.4
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(nycflights13)
library(RColorBrewer)
head(planes)
## # A tibble: 6 x 9
## tailnum year type manufacturer model engines seats speed engine
## <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
## 1 N10156 2004 Fixed wing mu~ EMBRAER EMB-1~ 2 55 NA Turbo-~
## 2 N102UW 1998 Fixed wing mu~ AIRBUS INDUST~ A320-~ 2 182 NA Turbo-~
## 3 N103US 1999 Fixed wing mu~ AIRBUS INDUST~ A320-~ 2 182 NA Turbo-~
## 4 N104UW 1999 Fixed wing mu~ AIRBUS INDUST~ A320-~ 2 182 NA Turbo-~
## 5 N10575 2002 Fixed wing mu~ EMBRAER EMB-1~ 2 55 NA Turbo-~
## 6 N105UW 1999 Fixed wing mu~ AIRBUS INDUST~ A320-~ 2 182 NA Turbo-~
head(flights)
## # A tibble: 6 x 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 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## # ... with 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>
pasg_flt<- flights %>%
left_join(planes, by = "tailnum") %>%
group_by(month, dest) %>%
replace_na(list(seats = 0)) %>%
#Estimating 75% capacity for planes
mutate(passenger = seats * .75) %>%
summarise(num_passenger = round(sum(passenger)))
## `summarise()` has grouped output by 'month'. You can override using the `.groups` argument.
num_dest <- flights %>%
#Counting the number of flights by month and destination
group_by(month, dest) %>%
count(dest) %>%
rename("num_of_flt" = n) %>%
ungroup()%>%
#Subsetting top 10 destinations by month
arrange(month,desc(num_of_flt)) %>%
slice(1:10, 95:104,187:196, 282:291, 375:384, 465:474, 558:567, 652:661, 745: 754, 837:846, 928:937, 1018:1027)
#Joining the two previous tables
flt_stat<- num_dest %>%
inner_join(pasg_flt, by = c("month", "dest"))%>%
#Creating a month column and converting into a factor
mutate(MonthName = month.name[month]) %>%
mutate(ordered_month = factor(MonthName, levels = month.name, ordered = TRUE))
flt_stat %>%
ggplot(aes(x = dest, y = num_of_flt, fill = num_passenger)) +
geom_col() +
xlab("Top Destinations") +
ylab("Number of Flights")+
ggtitle("Top 10 New York Area Airports Destinations By Month")+
labs(fill = "Traveler Estimate")+
coord_flip() +
theme(axis.text.x = element_text(angle = 90), axis.text.y = element_text(size = 7))+
facet_wrap(~ordered_month)+
scale_fill_gradient(low = "#e0ecf4", high = "#8856a7")
The faceted bar charts display the top 10 destinations by month of flights departing from airports located in the New York area in the year 2013. The popular destinations are signified by their airport codes which are located along the left side of the graph. The number of flights to those popular destinations are located at the bottom of the graph. The popular destinations and number of flights were determined using the flights dataset in the nycflights13 package. The legend on the right displays a color gradient scale estimating number of travelers headed to a destination if the planes departing the New York area airports were filled to seventy-five percent capacity. The capacity of the planes traveling to the popular destinations were determined using the planes dataset in the nycflights13 package. The travel patterns displayed by the graph demonstrated that Los Angeles airport was the most popular destination from the New York area airports. Chicago had a higher number of flights compared to Los Angeles throughout the year, but the estimation of travelers showed that more people were possibly traveling to Los Angeles in comparison to Chicago.