-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_dataviz.R
More file actions
218 lines (189 loc) · 7.54 KB
/
project_dataviz.R
File metadata and controls
218 lines (189 loc) · 7.54 KB
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
library(nycflights13)
library(tidyverse)
library(patchwork)
##flight_avg_per_day
flight_avg_per_day <-flights%>%
select(year,month,day,origin)%>%
group_by(year,month,day,origin)%>%
summarise(total = n())%>%
ungroup()%>%
mutate(day_name = c(rep(c("Tuesday","Tuesday","Tuesday","Wednesday","Wednesday","Wednesday","Thursday","Thursday","Thursday","Friday","Friday","Friday","Saturday","Saturday","Saturday","Sunday","Sunday","Sunday","Monday","Monday","Monday"),52),"Tuesday","Tuesday","Tuesday"))%>%
group_by(day_name,origin)%>%
summarise(count =n(),
total = sum(total),
avg = total/count)
ggplot(flight_avg_per_day, aes(day_name,avg))+
geom_bar(stat = "identity",aes(fill = factor(day_name)))+
facet_wrap(~origin,ncol =1)+
ylim(0,500)+
labs(title = "Average people in each name and each airport 2013",
x = "days",
y = "average_people",
caption = "Source : nycflights13 package",
fill = "Day\n")+
theme(plot.title = element_text(size = 20))
test2 <- flight_avg_per_day %>%
group_by(day_name) %>%
summarise(avg = sum(avg)) %>%
arrange(desc(avg))
test2$day_name <- factor(test2$day_name,
levels = test2$day_name)
ggplot(test2, aes(day_name,avg))+
geom_bar(stat = "identity",aes(fill = factor(day_name)))+
ylim(0,1200)+
labs(title = "Average people each name day at LGA airport 2013",
x = "days",
y = "average_people",
caption = "Source : nycflights13 package",
fill = "Day\n")+
theme(plot.title = element_text(size = 20))
##Competition in each airport
carrier_num <- flights %>%
count(carrier)%>%
arrange(desc(n))
ggplot(flights,aes(x=reorder(carrier,carrier,
function(x)-length(x))))+
geom_bar()+
facet_wrap(~origin)+
labs(title = "flight per airline each airport 2013",
x = "airlines",
y = "total flights",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
##zoom to LGA
flights_LGA <- flights %>%
filter(origin == "LGA",carrier %in% LGA$carrier)%>%
group_by(carrier)%>%
summarise(n = n())%>%
arrange(desc(n))
flights_LGA$carrier <- factor(flights_LGA$carrier,
levels = flights_LGA$carrier)
ggplot(flights_LGA,aes(x = carrier,y=n,fill = factor(carrier))) +
geom_bar(stat = "identity")+
labs(title = "flight per airline at LGA airport 2013",
x = "airlines",
y = "total flights",
fill = "Airlines",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
##dest_LGA
dest_LGA <- flights %>%
filter(origin == "LGA") %>%
group_by(dest)%>%
summarise(total_flight =n())%>%
arrange(desc(total_flight))%>%
head(15)
dest_LGA$dest = factor(dest_LGA$dest,
levels =dest_LGA$dest)
ggplot(dest_LGA,aes(dest,total_flight))+
geom_bar(stat = "identity",aes(fill = factor(dest)))+
labs(title = "Flight each destination at LGA airport 2013",
x = "Destination",
y = "Total_Flights",
caption = "Source : nycflights13 package",
fill = "Destination")+
theme(plot.title = element_text(size = 20))
##check
check1 <- flights %>%
filter(origin == "LGA", dest %in% dest_LGA$dest[1:5])%>%
group_by(carrier,dest)%>%
summarise(total_flight = n())%>%
arrange(desc(total_flight))
check1$carrier <- factor(check1$carrier,
levels = flights_LGA$carrier)
ggplot(check1,aes(carrier,total_flight,fill=dest))+
geom_bar(stat = "identity")+
labs(title = "Total flight Top 5 destination each airlines at LGA Airport 2013",
x = "airlines",
y = "Total_Flights",
caption = "Source : nycflights13 package",
fill = "Airlines")+
theme(plot.title = element_text(size = 20))
##total_plane
tailnum_air <- flights%>%
filter(origin == "LGA")%>%
select(carrier,tailnum)%>%
group_by(carrier)%>%
summarise(total_plane = n_distinct(tailnum))%>%
arrange(desc(total_plane))
tailnum_air$carrier <- factor(tailnum_air$carrier,
levels = flights_LGA$carrier)
ggplot(tailnum_air,aes(carrier,total_plane))+
geom_bar(stat="identity",fill="#5ec8d8")+
labs(title = "Total plane each airline at LGA airport 2013",
x = "Airline",
y = "Total_Planes",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
##seat_per_airline
tail_LGA <- flights %>%
filter(origin =="LGA")%>%
left_join(planes,by= c("tailnum"="tailnum"))
seat_LGA <- tail_LGA %>%
select(carrier,seats,model)%>%
mutate(na_seat = ifelse(is.na(seats)== FALSE,TRUE,FALSE))%>%
group_by(carrier)%>%
summarise(all_seat = sum(seats,na.rm=TRUE),
unit = sum(na_seat),
seat_per_plane = all_seat/unit)%>%
arrange(desc(seat_per_plane))
seat_LGA$carrier = factor(seat_LGA$carrier,
levels = seat_LGA$carrier)
ggplot(seat_LGA,aes(carrier,seat_per_plane))+
geom_bar(stat="identity",fill="#5ec8d8")+
labs(title = "Seat per plane each airline at LGA airport 2013",
x = "carrier",
y = "seat_per_plane",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
#check
seat_LGA$carrier = factor(seat_LGA$carrier,
levels = flights_LGA$carrier)
ggplot(seat_LGA,aes(carrier,seat_per_plane))+
geom_bar(stat="identity",fill="#5ec8d8")+
labs(title = "Seat per plane each airline at LGA airport 2013",
x = "carrier",
y = "seat_per_plane",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
##delay
delay <-flights %>%
filter(origin == "LGA") %>%
select(4:9,10,15:16)%>%
mutate(delay = ifelse(arr_delay+dep_delay>0,TRUE,FALSE))%>%
filter(is.na(delay) == FALSE)%>%
group_by(carrier) %>%
summarise(total_flight = n(),
delay = sum(delay,na.rm = TRUE),
avg_arr_come =mean(arr_delay+dep_delay,na.rm = TRUE),
avg_arr_delay =mean(ifelse(arr_delay+dep_delay> 15,arr_delay+dep_delay,0),na.rm = TRUE),
delay_ratio = delay/total_flight) %>%
select(1:2,4:6)%>%
arrange(avg_arr_come,avg_arr_delay)
ggplot(delay,aes(total_flight,avg_arr_delay))+
geom_point(size = 3)+
geom_smooth(method = "lm")+
labs(title = "Corelation about total flight and average delay each airline at LGA airport 2013",
x = "total_flights",
y = "averange delay",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 15))
delay$carrier <- factor(delay$carrier,
levels = flights_LGA$carrier)
gr1 <- ggplot(delay)+
geom_point(data = delay,aes(x = carrier,y = delay_ratio),size = 4,color ="red")+
geom_line(data = delay,aes(x = carrier,y = delay_ratio),color = "black",size = 1,group = 1)+
ylim(c(0.1,0.8))+
labs(title = "Delay Ratio each airline at LGA airport 2013",
x = "Carrier",
y = "Delay_Ratio",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
gr2 <- ggplot(delay,aes(carrier,total_flight))+
geom_bar(stat = "identity",fill ="#5875e8")+
labs(title = "Total flight each airlines at LGA airport 2013",
x = "Carrier",
y = "Total_Flights",
caption = "Source : nycflights13 package")+
theme(plot.title = element_text(size = 20))
gr1+gr2