``` title: “Airline Reliability Dashboard New York Flights 2013” output: flexdashboard::flex_dashboard: orientation: columns vertical_layout: fill

Column

Chart1

Proportion of Flights Delayed (Departure and Arrival) by Carrier

By default, each level has markdown headers within dashboards define

column, with individual charts stacked vertically within each colomun

group_by(data, …), the data are taken from exixting tables

n() is the number of observations in the current group, only can be

used from within summarise(), mutate() and filter()

round () is to return to the nearest even number

flights_delay <- flights_new %>% group_by(carrier) %>%  
  summarise(dep_del = sum(dep_del_status,na.rm = TRUE),
            arr_del = sum(arr_del_status,na.rm = TRUE),
            n = n(),
            prop_dep_del = round(dep_del/n,2),    
            prop_arr_del = round(arr_del/n,2)
  )
  
### fct_reorder() is useful for 1d displays the levels: 
### flights_delay$carrier, and flights_delay $ prop_dep_del in match the order of the first 
### apperarance in data where the factor is mapped to position
flights_delay$carrier <- fct_reorder(flights_delay$carrier, flights_delay$prop_dep_del)

### Get special words so called as the key word "Delay", and gather columns into 
### key - value pairs from 5 : 6  
flights_delay_long <- flights_delay %>% gather(key = "Delay", value = "prop", 5:6)

flights_delay_long$Delay <- factor(flights_delay_long$Delay,
                                   levels = c("prop_dep_del","prop_arr_del"),
                                   labels = c("Departure","Arrival"),
                                   order = TRUE)

p1 <- ggplot(flights_delay_long, aes(x = carrier, y = prop)) + 
  geom_bar(stat = "identity", fill = "steelblue") + facet_grid(.~Delay) +
  labs(x = "Carrier Code", y = "Proportion on time (+/- 15 mins of schedule)") +
  theme_grey()
ggplotly(p1)  

Column (data-width=350)

Chart 2

Proportion of Flights Delayed (Departure) by Carrier by Month

flights_delay_month <- flights_new %>% group_by(carrier,month) %>% 
  summarise(dep_del = sum(dep_del_status,na.rm = TRUE),
            arr_del = sum(arr_del_status,na.rm = TRUE),
            n = n(),
            prop_dep_del = round(dep_del/n,2),
            prop_arr_del = round(arr_del/n,2)
  )

flights_delay_month$carrier <- fct_reorder(flights_delay_month$carrier, flights_delay_month$prop_dep_del)

p2 <- ggplot(flights_delay_month, aes(x = carrier, y = as.factor(month))) + 
  geom_tile(aes(fill = prop_dep_del)) + 
  scale_fill_gradient(name = "Proportion", low = "white",high = "steelblue") +
  labs(x = "Carrier", y = "Month")
ggplotly(p2)

Chart 3

Proportion of Flights Delayed (Arrival) by Carrier by Month

flights_delay_month <- flights_new %>% group_by(carrier,month) %>% 
  summarise(dep_del = sum(dep_del_status,na.rm = TRUE),
            arr_del = sum(arr_del_status,na.rm = TRUE),
            n = n(),
            prop_dep_del = round(dep_del/n,2),
            prop_arr_del = round(arr_del/n,2)
  )

flights_delay_month$carrier <- fct_reorder(flights_delay_month$carrier, flights_delay_month$prop_dep_del)

p3 <- ggplot(flights_delay_month, aes(x = carrier, y = as.factor(month))) + 
  geom_tile(aes(fill = prop_arr_del)) + 
  scale_fill_gradient(name = "Proportion", low = "white",high = "steelblue") +
  labs(x = "Carrier", y = "Month")

  ggplotly(p3)