-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_plots.R
276 lines (207 loc) · 7.13 KB
/
graph_plots.R
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
###########################################################################################-
###########################################################################################-
##
## graph plots ----
##
###########################################################################################-
###########################################################################################-
#=========================================================================================#
# Setting up ----
#=========================================================================================#
#-----------------------------------------------------------------------------------------#
# Loading libraries
#-----------------------------------------------------------------------------------------#
library(tidyverse)
library(lubridate)
library(glue)
library(hms)
library(here)
library(DBI)
library(RSQLite)
library(dbplyr)
library(viridis)
library(fs)
library(ggspatial)
library(ggdark)
library(tictoc)
library(sf)
library(extrafont)
loadfonts("win")
#-----------------------------------------------------------------------------------------#
# Setting map view parameters
#-----------------------------------------------------------------------------------------#
# Bounding box around LGA (big enough to show aproaches from all directions)
lga_bbox <-
tibble(
longitude = c(-74.232575, -73.516318),
latitude = c(40.503766, 41.046881)
)
# Where the runways cross
lga_center <-
c(
longitude = -73.874861,
latitude = 40.780347
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# CRS
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
my_crs <- "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
#-----------------------------------------------------------------------------------------#
# Pulling data
#-----------------------------------------------------------------------------------------#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Connecting to database
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
lga_tracks_db <- dbConnect(SQLite(), "data/lga_tracks_db.sqlite3")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Pulling
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
dates_to_plot <-
c(
seq(as_date("2020-03-16"), as_date("2020-03-20"), "day"),
seq(as_date("2020-04-06"), as_date("2020-04-10"), "day")
) %>%
as.numeric()
arrivals_tracks <-
lga_tracks_db %>%
tbl("arrivals_tracks") %>%
filter(flight_date %in% dates_to_plot) %>%
select(
time,
longitude,
latitude,
unique_flight,
month,
day,
hour,
wday
) %>%
filter(
longitude %>% between(!!lga_bbox$longitude[1], !!lga_bbox$longitude[2]),
latitude %>% between(!!lga_bbox$latitude[1], !!lga_bbox$latitude[2])
) %>%
collect() %>%
mutate(time = as_datetime(time, tz = "US/Eastern")) %>%
# Records with missing values are probably not "real" data points, so dropping them
drop_na(time, longitude, latitude, unique_flight) %>%
group_by(unique_flight) %>%
filter(time == max(time))
# Restricting path data to coordinates inside the bounding box, to reduce unnecessary processing and
# memory overhead, and also so that the eventual summary statistics correspond to paths that
# are visible on the map
min_max_seq_df <-
tibble(
time =
seq(
update(min(arrivals_tracks$time), hours = 0, minutes = 0, seconds = 0),
update(max(arrivals_tracks$time), hours = 24, minutes = 0, seconds = 0),
"1 hour"
)
) %>%
filter(as_date(time) %in% dates_to_plot)
arrivals_tracks_summarized <-
arrivals_tracks %>%
ungroup() %>%
mutate(floor_time = floor_date(time, unit = "1 hour")) %>%
distinct(unique_flight, floor_time, .keep_all = TRUE) %>%
count(floor_time, name = "flights_count") %>%
left_join(
min_max_seq_df,
.,
by = c("time" = "floor_time")
) %>%
mutate(
month = month(time),
day = day(time),
hour = hour(time),
wday = wday(time),
date = as_date(time)
) %>%
mutate(flights_count = replace_na(flights_count, 0))
daily_average <-
arrivals_tracks_summarized %>%
group_by(date) %>%
summarise(
flights_mean = mean(flights_count, na.rm = TRUE),
flights_count = sum(flights_count, na.rm = TRUE)
) %>%
mutate(flights_count = str_c(flights_count, c(" flights", rep("", nrow(.) - 1))))
#=========================================================================================#
# Graph ----
#=========================================================================================#
#-----------------------------------------------------------------------------------------#
# Drawing graphs
#-----------------------------------------------------------------------------------------#
flights_per_hour <-
arrivals_tracks_summarized %>%
ggplot() +
# 0-point
geom_hline(yintercept = 0, color = "gray20", linetype = 1, size = 1) +
# Daily average
geom_hline(
data = daily_average,
aes(
yintercept = flights_mean,
group = 1,
colour = "Daily average"
),
size = 1
) +
# Count in each minute
geom_path(
aes(
x = hour,
y = flights_count,
group = 1,
colour = "Each hour"
),
size = 1
) +
geom_label(
data = daily_average,
aes(x = 22, y = 45, label = flights_count),
color = "black",
fill = "white",
hjust = c(0.8, rep(0.5, nrow(daily_average) - 1)),
vjust = "middle",
label.r = unit(0.01, "lines"),
label.size = 1,
size = 3,
family = "DejaVu Sans Mono - Bront"
) +
scale_colour_manual(
name = NULL,
breaks = c("Daily average", "Each hour"),
values = c("#424186", "#FDE725"),
guide = guide_legend(direction = "vertical", override.aes = aes(size = 1))
) +
# Plot display specs
scale_y_continuous(name = "Flights / Hour", breaks = seq(0, 50, 10)) +
scale_x_continuous(name = "Hour", breaks = seq(3, 21, 3)) +
coord_cartesian(
xlim = c(0, 24),
ylim = c(0, 50),
expand = FALSE
) +
dark_theme_gray() +
# Tweaking the plot display
theme(
legend.justification = c(0, 1),
legend.position = c(0, 1),
legend.background = element_rect(fill = NA, color = NA),
legend.title = element_blank(),
legend.margin = margin(t = 0, r = 0, b = 0, l = 3),
legend.key = element_rect(fill = NA),
text = element_text(size = 12, family = "DejaVu Sans Mono - Bront"),
legend.text = element_text(size = 9),
panel.grid.minor = element_blank()
) +
facet_wrap(vars(date), ncol = 5, labeller = label_bquote(cols = .(format(date, format = "%A, %x")))) +
labs(title = "Arrivals at LaGuardia airport, 3 weeks apart", caption = "Data scraped from FlightAware.com")
ggsave(
"plots/flights_per_hour.png",
flights_per_hour,
width = 15,
height = 5,
dpi = 200
)