data(nycflights)
ggplot(data = nycflights, aes(x = dep_delay)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data = nycflights, aes(x = dep_delay)) +
geom_histogram(binwidth = 15)
ggplot(data = nycflights, aes(x = dep_delay)) +
geom_histogram(binwidth = 150)
The lower the bindwith, the more specific the graph. A higher bindwith would potentially be useful for presenting data in simpiler models, but an appropriatly low bandwith creates the most accurate data.
data(nycflights)
lax_flights <- nycflights %>%
filter(dest == "LAX")
ggplot(data = lax_flights, aes(x = dep_delay)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
lax_flights %>%
summarise(mean_dd = mean(dep_delay),
median_dd = median(dep_delay),
n = n())
## # A tibble: 1 × 3
## mean_dd median_dd n
## <dbl> <dbl> <int>
## 1 9.78 -1 1583
sfo_feb_flights <- nycflights %>%
filter(dest == "SFO", month == 2)
sfo_feb_flights %>%
summary(sfo_feb_flights)
## year month day dep_time dep_delay
## Min. :2013 Min. :2 Min. : 1.00 Min. : 613 Min. :-10.0
## 1st Qu.:2013 1st Qu.:2 1st Qu.: 7.00 1st Qu.: 943 1st Qu.: -5.0
## Median :2013 Median :2 Median :16.00 Median :1268 Median : -2.0
## Mean :2013 Mean :2 Mean :15.26 Mean :1298 Mean : 10.5
## 3rd Qu.:2013 3rd Qu.:2 3rd Qu.:22.50 3rd Qu.:1742 3rd Qu.: 9.0
## Max. :2013 Max. :2 Max. :28.00 Max. :2159 Max. :209.0
## arr_time arr_delay carrier tailnum
## Min. : 118 Min. :-66.00 Length:68 Length:68
## 1st Qu.:1233 1st Qu.:-21.25 Class :character Class :character
## Median :1497 Median :-11.00 Mode :character Mode :character
## Mean :1607 Mean : -4.50
## 3rd Qu.:2062 3rd Qu.: 2.00
## Max. :2256 Max. :196.00
## flight origin dest air_time
## Min. : 11.0 Length:68 Length:68 Min. :317.0
## 1st Qu.: 85.0 Class :character Class :character 1st Qu.:345.0
## Median : 641.0 Mode :character Mode :character Median :354.0
## Mean : 795.1 Mean :351.9
## 3rd Qu.:1487.2 3rd Qu.:360.0
## Max. :2126.0 Max. :376.0
## distance hour minute
## Min. :2565 Min. : 6.00 Min. : 1.00
## 1st Qu.:2586 1st Qu.: 9.00 1st Qu.:25.00
## Median :2586 Median :12.50 Median :33.50
## Mean :2584 Mean :12.62 Mean :36.35
## 3rd Qu.:2586 3rd Qu.:17.00 3rd Qu.:54.00
## Max. :2586 Max. :21.00 Max. :59.00
ggplot(data = sfo_feb_flights, aes(x = dep_delay)) +
geom_histogram(binwidth = 2)
sfo_feb_flights %>%
summarise(mean_dd = mean(dep_delay),
median_dd = median(dep_delay))
## # A tibble: 1 × 2
## mean_dd median_dd
## <dbl> <dbl>
## 1 10.5 -2
68 flights flew from NYC to San Franciso in Febuary
The distrubuiton of delay times of flights headed to San Francisco is strange; a majority of the flights actually left early, (the median is -2), but the average delay time is actually 10 minutes over because of extreme outliers.
data(nycflights)
sfo_feb_flights <- nycflights %>%
filter(dest == "SFO", month == 2)
sfo_feb_flights %>%
group_by(origin) %>%
summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())
## # A tibble: 2 × 4
## origin median_dd iqr_dd n_flights
## <chr> <dbl> <dbl> <int>
## 1 EWR 0.5 5.75 8
## 2 JFK -2.5 15.2 60
sfo_feb_flights %>%
group_by(carrier) %>%
summarise(median_dd = median(dep_delay), iqr_dd = IQR(dep_delay), n_flights = n())
## # A tibble: 5 × 4
## carrier median_dd iqr_dd n_flights
## <chr> <dbl> <dbl> <int>
## 1 AA 13 32.8 10
## 2 B6 -2 3.5 6
## 3 DL -3 6.5 19
## 4 UA -2 13 21
## 5 VX -3.5 16.8 12
The carrier AA has the highest median delay and highest Inter quartile range
nycflights %>%
group_by(month) %>%
summarise(mean_dd = mean(dep_delay)) %>%
arrange(desc(mean_dd))
## # A tibble: 12 × 2
## month mean_dd
## <int> <dbl>
## 1 7 20.8
## 2 6 20.4
## 3 12 17.4
## 4 4 14.6
## 5 3 13.5
## 6 5 13.3
## 7 8 12.6
## 8 2 10.7
## 9 1 10.2
## 10 9 6.87
## 11 11 6.10
## 12 10 5.88
A lower median depature could potentially mean your more likely to get a flight with a higher delay time compared to a lower mean, but if a month tends to run the risk of having an extremely long depature delay then that risk would be modeled in the average.
nycflights <- nycflights %>%
mutate(dep_type = ifelse(dep_delay < 5, "on time", "delayed"))
nycflights %>%
group_by(origin) %>%
summarise(ot_dep_rate = sum(dep_type == "on time") / n()) %>%
arrange(desc(ot_dep_rate))
## # A tibble: 3 × 2
## origin ot_dep_rate
## <chr> <dbl>
## 1 LGA 0.728
## 2 JFK 0.694
## 3 EWR 0.637
ggplot(data = nycflights, aes(x = origin, fill = dep_type)) +
geom_bar()
LGA is the least likely to be delayed.
nycflights <- nycflights %>%
mutate(air_speed = distance / (air_time / 60))
nycflights %>%
group_by(origin) %>%
summarise(distance = sum(dep_type == "on time") / n()) %>%
arrange(desc(distance))
## # A tibble: 3 × 2
## origin distance
## <chr> <dbl>
## 1 LGA 0.728
## 2 JFK 0.694
## 3 EWR 0.637
ggplot(data = nycflights, aes(x = distance, y = air_speed)) +
geom_point()
library(ggplot2)
colors <- c('AA' = 'red', 'DL' = 'green', 'UA' = 'blue')
nycflights_carry <- nycflights %>%
filter(carrier %in% c("AA", "DL", "UA"))
ggplot(data = nycflights_carry, aes(x = dep_delay, y = arr_delay, color = carrier)) +
geom_point(size = 3) +
scale_color_manual(values = colors) +
labs(
x = 'dep_delay',
y = 'arr_delay',
title = 'Dot Plot with Colors based on Carrier'
)