-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDA_backup.Rmd
824 lines (612 loc) · 22 KB
/
EDA_backup.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
---
title: "EDA"
author: "Si Chen"
date: "`r Sys.Date()`"
output: github_document
---
```{r load_library, warning = FALSE, message = FALSE}
library(tidyr)
library(tidyverse)
library(rvest)
library(dplyr)
library(cowplot)
library(gridExtra)
library(RColorBrewer)
library(plotly)
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
fig.width = 8,
fig.height = 6,
out.width = "90%"
)
theme_set(theme_minimal() + theme(legend.position = "bottom"))
options(
ggplot2.continuous.colour = "viridis",
ggplot2.continuous.fill = "viridis"
)
scale_colour_discrete = scale_colour_viridis_d
scale_fill_discrete = scale_fill_viridis_d
```
### Load clean datasets
```{r load_data}
df_2013 =
read_csv("data/merge_data_2013.csv", show_col_types = FALSE)
df_2017 =
read_csv("data/merge_data_2017.csv", show_col_types = FALSE)
# Convert month to factor with levels in ascending order and labels as month abbreviations
df_2013$month <- factor(df_2013$month, levels = 1:12, labels = month.abb[1:12])
df_2013 <- df_2013 %>%
mutate(date = paste(month, day, sep = "_"))
```
```{r agg_date}
average_delay_by_date <- df_2013 %>%
# group_by(date) %>%
group_by(month, date) %>%
summarise(
avg_arr_delay = mean(arr_delay, na.rm = TRUE),
avg_precip = mean(precip, na.rm = TRUE),
avg_wind_dir = mean(wind_dir, na.rm = TRUE),
avg_wind_speed = mean(wind_speed, na.rm = TRUE),
avg_wind_gust = mean(wind_gust, na.rm = TRUE),
avg_pressure = mean(pressure, na.rm = TRUE),
avg_visib = mean(visib, na.rm = TRUE)
)
```
### Data set description
In the dataset for 2013, there are `r ncol(df_2013)` columns and `r nrow(df_2013)` rows.
In the dataset for 2017, there are `r ncol(df_2017)` columns and `r nrow(df_2017)` rows.
There are `r n_distinct(df_2013$carrier)` different carriers flying out of NYC airports. NYC has `r n_distinct(df_2013$origin)` different airports. There are `r n_distinct(df_2013$dest)` different destination locations to which flights fly out of NYC airports.
TBC...
# Weather
Visualize association between weather variables?
aggregate by date, plot scatter plot, add fitted line (instead of by month)?
```{r aggregate_df_2013_month}
# Aggregate the data by month and calculate averages
df_2013_avg <- df_2013 %>%
group_by(month) %>%
summarize(
avg_arr_delay = mean(arr_delay, na.rm = TRUE),
avg_precip = mean(precip, na.rm = TRUE),
avg_wind_dir = mean(wind_dir, na.rm = TRUE),
avg_wind_speed = mean(wind_speed, na.rm = TRUE),
avg_wind_gust = mean(wind_gust, na.rm = TRUE),
avg_pressure = mean(pressure, na.rm = TRUE),
avg_visib = mean(visib, na.rm = TRUE)
)
```
## Preciptation 2013
```{r precip, echo=FALSE}
# Plot precipitation against month using bar plot
precip_bar =
df_2013 %>%
ggplot(aes(x = month, y = precip)) +
geom_bar(stat = "summary", fun = "mean", fill = "skyblue", alpha = 0.7) +
labs(title = "Average Precipitation by Month",
x = "Month",
y = "Precipitation (inches)")
# Plot average precipitation by month in scatter plot + line
precip_line =
ggplot(df_2013, aes(x = month, y = precip)) +
geom_point(stat = "summary", fun = "mean", size = 3, color = "coral") +
geom_line(aes(group = 1), stat = "summary", fun = "mean", color = "skyblue", size = 1) +
labs(title = "Average Precipitation by Month",
x = "Month",
y = "Precipitation (inches)")
# precipitation by month in violin plot
precip_violin =
ggplot(df_2013, aes(x = month, y = precip, color = month)) +
geom_violin(alpha = 0.7) +
labs(title = "Distribution of Precipitation by Month",
x = "Month",
y = "Precipitation (inches)")
# Plot bar plot with line overlay
precip_line_bar =
ggplot(df_2013, aes(x = month, y = precip)) +
geom_bar(aes(x=month, y=precip),stat="summary", fun = 'mean', fill="skyblue")+
geom_point(aes(x=month, y=precip), stat = "summary", fun = "mean", size = 2, color = "coral", alpha = .5) +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="coral", size=1, alpha = .8)+
labs(
title= "Average Precipitation by Month",
x="Month",
y="Precipitation")
```
```{r precip_delay}
# Plot precipitation against arrival delay in a scatter plot
precip_delay =
ggplot(df_2013, aes(x = precip, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Precipitation against Arrival Delay",
x = "Precipitation (inches)",
y = "Arrival Delay (minutes)")
```
```{r precip_delay_month}
# Average precip against average arr_delay by month
precip_delay_month =
ggplot(df_2013_avg, aes(x = avg_precip, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Precipitation against Arrival Delay",
x = "Precipitation (inches)",
y = "Arrival Delay (minutes)")
```
```{r precip_delay_date}
precip_delay_date =
ggplot(average_delay_by_date, aes(x = avg_precip, y = avg_arr_delay, color = month)) +
geom_point(size = 3, alpha = .6) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Precipitation against Arrival Delay by date",
x = "Precipitation (inches)",
y = "Arrival Delay (minutes)")
precip_delay_date
```
```{r show_precip}
# grid.arrange(precip_line_bar, precip_delay, nrow = 2)
# precip_violin
# precip_line
# precip_bar
summary(df_2013$precip)
precip_line_bar
precip_delay
precip_delay_month
# precip_delay_date
```
## Pressure
```{r plot_pressure}
# Plot average pressure by month in line plot
pressure_line =
ggplot(df_2013, aes(x = month, y = pressure)) +
#geom_bar(aes(x=month, y=pressure),stat="summary", fun = 'mean', fill="skyblue")+
geom_point(aes(x=month, y=pressure), stat = "summary", fun = "mean", color = "coral", alpha = .8) +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="skyblue", size=1, alpha = .8) +
labs(
title= "Average Pressure by Month",
x="Month",
y="Pressure")
# Plot Pressure against arrival delay in a scatter plot
pressure_delay =
ggplot(df_2013, aes(x = pressure, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Pressure against Arrival Delay",
x = "Pressure",
y = "Arrival Delay (minutes)")
# Average pressure against average arr_delay by month
pressure_delay_month =
ggplot(df_2013_avg, aes(x = avg_pressure, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Pressure against Arrival Delay by Month",
x = "Avg Monthly Pressure",
y = "Avg Arrival Delay (minutes)")
```
```{r pressure_date}
# Average pressure against average arr_delay by month
pressure_delay_date =
ggplot(average_delay_by_date, aes(x = avg_pressure, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Pressure against Arrival Delay by Date",
x = "Avg Pressure by Date",
y = "Avg Arrival Delay (minutes)")
pressure_delay_date
```
```{r show_pressure}
summary(df_2013$pressure)
pressure_line
pressure_delay
pressure_delay_month
```
## Visibility
```{r visib}
# Plot average Visibility by month in line plot
visib_line =
ggplot(df_2013, aes(x = month, y = visib)) +
# geom_bar(aes(x=month, y=visib),stat="summary", fun = 'mean', fill="skyblue")+
geom_point(aes(x=month, y=visib), stat = "summary", fun = "mean", color = "coral") +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="skyblue", size=1, alpha = .8) +
labs(
title= "Average Visibility by Month",
x="Month",
y="Visibility")
# Plot Visibility against arrival delay in a scatter plot
visib_delay =
ggplot(df_2013, aes(x = visib, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Visibility against Arrival Delay",
x = "Visibility",
y = "Arrival Delay (minutes)")
# Average Visibility against average arr_delay by month
visib_delay_month =
ggplot(df_2013_avg, aes(x = avg_visib, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Visibility against Arrival Delay by Month",
x = "Avg Monthly Visibility",
y = "Avg Arrival Delay (minutes)")
```
```{r visib_date}
# Average Visibility against average arr_delay by date
visib_delay_date =
ggplot(average_delay_by_date, aes(x = avg_visib, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Visibility against Arrival Delay by Date",
x = "Avg Visibility by Date",
y = "Avg Arrival Delay (minutes)")
visib_delay_date
```
```{r show_visib}
summary(df_2013$visib)
visib_line
visib_delay
visib_delay_month
```
## Wind Direction
```{r plot_wind_dir}
# Plot average wind_dir by month in line plot
wind_dir_line =
ggplot(df_2013, aes(x = month, y = wind_dir)) +
geom_bar(aes(x=month, y=wind_dir),stat="summary", fun = 'mean', fill="skyblue", alpha = .8)+
geom_point(aes(x=month, y=wind_dir), stat = "summary", fun = "mean", color = "red") +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="coral", size=1, alpha = .9) +
labs(
title= "Average Wind Direction by Month",
x="Month",
y="Wind Direction")
# Plot Wind Direction against arrival delay in a scatter plot
wind_dir_delay =
ggplot(df_2013, aes(x = wind_dir, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Wind Direction against Arrival Delay",
x = "Wind Direction",
y = "Arrival Delay (minutes)")
# Average Wind Direction against average arr_delay by month
wind_dir_delay_month =
ggplot(df_2013_avg, aes(x = avg_wind_dir, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Direction against Arrival Delay by Month",
x = "Avg Monthly Wind Direction",
y = "Avg Arrival Delay (minutes)")
```
```{r wind_dir_date}
# Average Wind Direction against average arr_delay by date
wind_dir_delay_date =
ggplot(average_delay_by_date, aes(x = avg_wind_dir, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Direction against Arrival Delay by Date",
x = "Avg Daily Wind Direction",
y = "Avg Arrival Delay (minutes)")
wind_dir_delay_date
```
```{r show_wind_dir}
summary(df_2013$wind_dir)
wind_dir_line
wind_dir_delay
wind_dir_delay_month
```
## Wind Speed
```{r plot_wind_speed}
# Plot average wind_speed by month in line plot
wind_speed_line =
ggplot(df_2013, aes(x = month, y = wind_speed)) +
geom_bar(aes(x=month, y=wind_speed),stat="summary", fun = 'mean', fill="skyblue", alpha = .8)+
geom_point(aes(x=month, y=wind_speed), stat = "summary", fun = "mean", color = "red") +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="coral", size=1, alpha = .8) +
labs(
title= "Average Wind Speed by Month",
x="Month",
y="Wind Speed")
# Plot wind_speed against arrival delay in a scatter plot
wind_speed_delay =
ggplot(df_2013, aes(x = wind_speed, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Wind Speed against Arrival Delay",
x = "Wind Speed",
y = "Arrival Delay (minutes)")
# Average wind_speed against average arr_delay by month
wind_speed_delay_month =
ggplot(df_2013_avg, aes(x = avg_wind_speed, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Speed against Arrival Delay by Month",
x = "Avg Monthly Wind Speed",
y = "Avg Arrival Delay (minutes)")
```
```{r wind_speed_date}
# Average wind_speed against average arr_delay by date
wind_speed_delay_date =
ggplot(average_delay_by_date, aes(x = avg_wind_speed, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Speed against Arrival Delay by Date",
x = "Avg Daily Wind Speed",
y = "Avg Arrival Delay (minutes)")
wind_speed_delay_date
```
```{r show_wind_speed}
summary(df_2013$wind_speed)
wind_speed_line
wind_speed_delay
wind_speed_delay_month
```
## Wind Gust
```{r plot_wind_gust}
# Plot average wind_gust by month in line plot
wind_gust_line =
ggplot(df_2013, aes(x = month, y = wind_gust)) +
geom_bar(aes(x=month, y=wind_gust),stat="summary", fun = 'mean', fill="skyblue", alpha = .8)+
geom_point(aes(x=month, y=wind_gust), stat = "summary", fun = "mean", color = "red") +
geom_line(aes(group = 1), stat="summary", fun = 'mean', color="coral", size=1, alpha = .8) +
labs(
title= "Average Wind Gust by Month",
x="Month",
y="Wind Gust")
# Plot wind_gust against arrival delay in a scatter plot
wind_gust_delay =
ggplot(df_2013, aes(x = wind_gust, y = arr_delay)) +
geom_point(alpha = 0.5, color = "skyblue") +
labs(title = "Scatter Plot of Wind Gust against Arrival Delay",
x = "Wind Gust",
y = "Arrival Delay (minutes)")
# Average wind_gust against average arr_delay by month
wind_gust_delay_month =
ggplot(df_2013_avg, aes(x = avg_wind_gust, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Gust against Arrival Delay by Month",
x = "Avg Monthly Wind Gust",
y = "Avg Arrival Delay (minutes)")
```
```{r wind_gust_date}
# Average wind_gust against average arr_delay by date
wind_gust_delay_date =
ggplot(average_delay_by_date, aes(x = avg_wind_gust, y = avg_arr_delay, color = month)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Wind Gust against Arrival Delay by Date",
x = "Avg Daily Wind Gust",
y = "Avg Arrival Delay (minutes)")
wind_gust_delay_date
```
```{r show_wind_gust}
summary(df_2013$wind_gust)
wind_gust_line
wind_gust_delay
wind_gust_delay_month
```
## Weather Variables
```{r plot_weather}
combined_weather_1 <-
grid.arrange(
pressure_delay_date,
wind_dir_delay_date,
wind_gust_delay_date,
wind_speed_delay_date,
ncol = 2,
layout_matrix = matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE),
heights = c(1, 1), widths = c(1, 1),
top = "Weather Variables")
combined_weather_1
```
# Flights
## Arrival Delay Summary
```{r delay_summary}
summary(df_2013$arr_delay)
dd_hist =
ggplot(data = df_2013, aes(x = arr_delay)) +
geom_histogram(fill = 'skyblue', color = 'coral')+
labs(title = "Histogram of Arrival Delay",
x = "Arrival Delay (minutes)",
y = "Frequency")
dd_hist
```
## Distance - Arrival Delay - Destination
The relationship between the distance and the average delay for each location.
```{r dist_delay_dest}
df_2013_dest <-
df_2013 %>%
group_by(dest) %>%
summarize(
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE)
)
```
```{r}
df_2013_dest
ggplot(df_2013_dest, aes(dist, delay)) +
geom_point(aes(size = count, color = dist),alpha = 0.5) +
geom_smooth(color = 'skyblue') +
geom_jitter() +
scale_fill_brewer(palette = "Set3") +
labs(title = "Distance against Arrival Delay",
x = "Distance",
y = "Arrival Delay (minutes)")
```
Flights tend to have more delays in short-medium distances. Long distance flights do not have as much delay.
## Arrival delay by origins
```{r origin_delay}
df_2013 %>%
group_by(origin) %>%
summarise(mean_ad = mean(arr_delay), sd_ad = sd(arr_delay), IQR_ad = IQR(arr_delay), n = n()) %>%
arrange(desc(mean_ad))
# Plot violin plot of arr_delay against origin, coloring by origin
# reversing the order from high to low
delay_origin_box =
ggplot(df_2013, aes(x = reorder(origin, -arr_delay), y = arr_delay, fill = origin)) +
geom_boxplot(trim = FALSE, scale = "width", alpha = 0.7) +
labs(title = "Violin Plot of Arrival Delay by Origin",
x = "Origin",
y = "Arrival Delay (minutes)")
delay_origin_box
```
## Arrival delay by Destination
```{r dest_delay}
df_2013 %>%
group_by(dest) %>%
summarise(mean_ad = mean(arr_delay), sd_ad = sd(arr_delay), IQR_ad = IQR(arr_delay), n = n()) %>%
arrange(desc(mean_ad))
# Create a summary table with average arrival delay for each destination
avg_arr_delay <- df_2013 %>%
group_by(dest) %>%
summarise(avg_arr_delay = mean(arr_delay, na.rm = TRUE))
```
```{r}
# Create an interactive map using plotly
fig <- plot_ly(
data = avg_arr_delay,
type = 'scattergeo',
locationmode = 'USA-states',
locations = ~dest,
z = ~avg_arr_delay,
text = ~paste('Destination: ', dest, '<br>Avg Arrival Delay: ', round(avg_arr_delay, 2)),
color = ~avg_arr_delay,
colorscale = 'Viridis',
marker = list(line = list(width = 0.5, color = 'white'))
) %>%
layout(
geo = list(
scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = 'rgb(217, 217, 217)',
subunitwidth = 1,
countrywidth = 1,
subunitcolor = 'rgb(255,255,255)',
countrycolor = 'rgb(255,255,255)'
),
title = 'Average Arrival Delay by Destination',
xaxis = list(title = 'Destination'),
yaxis = list(title = 'Average Arrival Delay')
)
# Show the interactive map
fig
```
```{r plot_dest_delay}
# Calculate average arrival delay by destination
avg_arr_delay_by_dest <- df_2013 %>%
group_by(dest) %>%
summarize(avg_arr_delay_dest = mean(arr_delay, na.rm = TRUE)) %>%
arrange(desc(avg_arr_delay_dest)) %>%
head(10)
# Select only the top 6 destinations
top_destinations <- avg_arr_delay_by_dest$dest
# Filter the data for the top 6 destinations
filtered_data <- df_2013 %>% filter(dest %in% top_destinations)
# Create a boxplot for the top 6 destinations
top10_dest =
ggplot(filtered_data, aes(x = reorder(dest, -arr_delay), y = arr_delay, fill = dest)) +
geom_boxplot() +
scale_fill_brewer(palette = "Set3") +
labs(title = "Boxplot of Arrival Delay for Top 6 Destinations",
x = "Destination",
y = "Arrival Delay (minutes)")
top10_dest
```
## Arrival delay by carriers
```{r carrier_delay}
df_2013 %>%
group_by(carrier) %>%
summarise(mean_ad = mean(arr_delay), sd_ad = sd(arr_delay), IQR_ad = IQR(arr_delay), n = n()) %>%
arrange(desc(mean_ad))
# Plot violin plot of arr_delay against carrier, coloring by carrier
# reversing the order from high to low
delay_carrier_box =
ggplot(df_2013, aes(x = reorder(carrier, -arr_delay), y = arr_delay, fill = carrier)) +
geom_violin(trim = FALSE, scale = "width", alpha = 0.7) +
labs(title = "Violin Plot of Arrival Delay by Carrier",
x = "Carrier",
y = "Arrival Delay (minutes)")
delay_carrier_box
```
## Arrival Delay by Month
```{r month_delay}
df_2013 %>%
group_by(month) %>%
summarise(mean_ad = mean(arr_delay), sd_ad = sd(arr_delay), IQR_ad = IQR(arr_delay), n = n()) %>%
arrange(desc(mean_ad))
# Plot violin plot of arr_delay against month, coloring by month
# reversing the order from high to low
delay_month_box =
ggplot(df_2013, aes(x = reorder(month, -arr_delay), y = arr_delay, fill = month)) +
geom_violin(trim = FALSE, scale = "width", alpha = 0.7) +
labs(title = "Violin Plot of Arrival Delay by Month",
x = "Month",
y = "Arrival Delay (minutes)")
delay_month_box
```
## Flight
## Tailnum
```{r flight_delay}
flights_delay <-
df_2013 %>%
group_by(tailnum) %>%
summarize(
count = n(),
avg_delay = mean(arr_delay)
)
```
```{r}
flights_delay
ggplot(flights_delay, aes(avg_delay)) +
geom_freqpoly(color = 'skyblue') +
labs(title = "Distribution of Average Arrival Delay by Flight",
x = "Average Arrival Delay"
)
```
```{r find_max}
max_delay <-
flights_delay %>%
arrange(-avg_delay)
max_delay
```
N305AS had an average delay of 196 minutes, which is about 3 hours.
## On time arrival rate for NYC airports
Suppose a flight that is delayed for less than 5 minutes is basically “on time”. Assume we onsider any flight delayed for 5 minutes of more to be “delayed”
```{r on_time_arr}
df_2013 <-
df_2013 %>%
mutate(
ontime = arr_delay < 5
)
df_2013 <-
df_2013 %>%
mutate(
arr_type = ifelse(arr_delay < 5, "on time", "delayed")
)
df_2013 %>%
group_by(origin) %>%
summarise(ontime_prop = sum(ontime == TRUE) / n()) %>%
arrange(desc(ontime_prop))
ggplot(data = df_2013, aes(x = origin, fill = arr_type)) +
geom_bar()+
scale_fill_brewer(palette = "Set3") +
labs(title = "Bar Plot of On-time Arrival for NYC Airports",
x = "Origin",
y = "Count of on-time arrival")
```
The plot shows that LGA airport has the best on time arrival rate and EWR airport has the worst.
## Average Speed for flight (air_time & distance)
We set average speed = distance / (air_time/60) in miles/hr.
```{r avg_speed}
df_2013 <-
df_2013 %>%
mutate(
avg_speed = distance / (air_time/60)
)
ggplot(data = df_2013, aes(x = distance, y = avg_speed, color = avg_speed)) +
geom_point(alpha = .6) +
scale_fill_brewer(palette = "Set3") +
labs(title = "Scatter Plot of Average Flight Speed against Distance",
x = "Distance",
y = "Average Flight Speed")
ggplot(data = df_2013, aes(x = avg_speed, y = arr_delay, color = avg_speed)) +
geom_point(alpha = .4) +
scale_fill_brewer(palette = "Set3") +
geom_smooth(method = "lm", se = FALSE, color = "coral", size = 0.6) +
labs(title = "Scatter Plot of Average Flight Speed against Arrival Delay",
x = "Average Flight Speed",
y = "Arrival Delay (minutes)")
```