-
Notifications
You must be signed in to change notification settings - Fork 0
/
trip_dist.qmd
369 lines (307 loc) · 11.9 KB
/
trip_dist.qmd
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
---
title: "Mode share visualisation"
author: "Ali and Marina"
format:
html:
embed-resources: true
page-layout: full
editor: source
execute:
echo: false
warning: false
---
```{r}
require(tidyverse)
require(sf)
require(gt)
require(ggplot2)
require(dplyr)
require(stringr)
require(arrow)
require(plotly)
require(here)
# Set it to your local directory pointing to the General folder
per_dir_path <- "D:/Users/aa797/RMIT University/"
trips <- trips <- arrow::open_dataset(here(per_dir_path, "JIBE working group - General/manchester/simulationResults/ForUrbanTransition/visualization/trips.parquet"))
```
## Creating Visualizations
### Table of the number of trips in each local authority in Greater Manchester
```{r}
# Table of the number of trips in each local authority in Greater Manchester
trip_counts <- trips %>% to_duckdb() |>
select(t.id, LAD_origin, LAD_destination) %>%
pivot_longer(cols = c(LAD_origin, LAD_destination),
names_to = "type",
values_to = "location") %>%
group_by(location) %>%
summarise(trip_count = n()) %>%
ungroup() %>%
mutate(percent_of_total = (trip_count / sum(trip_count)))
trip_counts %>%
arrange(desc(percent_of_total)) %>%
select(location, percent_of_total) %>% # Exclude trip_count column
gt() %>%
tab_header(
title = "Trips by Location",
subtitle = "Percentage of Trips Within Local Authority Districts"
) %>%
cols_label(
location = "Location",
percent_of_total = "% of Total"
) %>%
fmt_percent(
columns = percent_of_total,
decimals = 1)
```
## Trips by Index of Multiple Deprivation (IMD) characteristics
```{r}
# Trips by various demographic characteristics
total_trips <- nrow(trips)
# Distribution of trips by mode and location
trips_percentage <- trips %>% to_duckdb() |>
group_by(LAD_origin, scenario) %>%
mutate(total_trips = n()) %>%
ungroup() %>%
group_by(LAD_origin, mode, scenario) %>%
summarise(trip_count = n(), total_trips = first(total_trips), .groups = 'drop') %>%
mutate(percentage_of_trips = (trip_count / total_trips) * 100) |> collect()
trips_percentage_all <- trips_percentage %>%
group_by(mode, scenario) %>%
summarise(trip_count = mean(trip_count),
total_trips = mean(total_trips),
percentage_of_trips = (trip_count / total_trips) * 100, .groups = 'drop') %>%
mutate(LAD_origin = "All Locations")
trips_percentage_combined <- bind_rows(trips_percentage |> collect(), trips_percentage_all |> collect())
ggplotly(ggplot(trips_percentage_combined) +
aes(x = mode, y = percentage_of_trips, fill = scenario) +
geom_col(position = "dodge2") +
geom_text(aes(label = round(percentage_of_trips, 1),
y = percentage_of_trips),
size = 2,
hjust = 1.1,
vjust = 0.2,
position = position_dodge(1),
inherit.aes = TRUE
) +
scale_fill_hue(direction = 1) +
coord_flip() +
theme_minimal() +
facet_wrap(vars(LAD_origin)) +
labs(
title = "% trips by mode and location",
fill = "Scenario")
)
# By IMD
trips_percentage_imd <- trips %>% to_duckdb() |>
group_by(imd_origin, scenario) %>%
mutate(total_trips = n()) %>%
ungroup() %>%
group_by(imd_origin, mode, scenario) %>%
summarise(trip_count = n(), total_trips = first(total_trips), .groups = 'drop') %>%
mutate(percentage_of_trips = (trip_count / total_trips) * 100) |> collect()
trips_percentage_imd$imd_origin <- as.factor(trips_percentage_imd$imd_origin)
trips_percentage_all_imd <- trips_percentage_imd %>%
group_by(mode, scenario) %>%
summarise(trip_count = mean(trip_count),
total_trips = mean(total_trips),
percentage_of_trips = (trip_count / total_trips) * 100, .groups = 'drop') %>%
mutate(imd_origin = "All IMDs") |> collect()
trips_percentage_combined_imd <- bind_rows(trips_percentage_all_imd,trips_percentage_imd)
trips_percentage_combined_imd$imd <- factor(trips_percentage_combined_imd$imd_origin,
levels = c("All IMDs", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
labels = c("All IMDs", "Most Deprived", "2", "3", "4", "5", "6", "7", "8", "9", "Least Deprived"))
ggplotly(
ggplot(trips_percentage_combined_imd, aes(x = mode, y = percentage_of_trips, fill = scenario)) +
geom_col(position = "dodge2") +
scale_fill_hue(direction = 1) +
geom_text(aes(label = round(percentage_of_trips, 1), y = percentage_of_trips),
size = 2, #hjust = -0.1,
hjust = 1.1,
vjust = 0.2,
position = position_dodge(1),
inherit.aes = TRUE
) +
coord_flip() +
theme_minimal() +
facet_wrap(vars(imd)) +
labs(
title = "% trips by mode IMD",
fill = "Scenario")
)
```
# Average weekly distance by mode of transportation
```{r}
pp=trips%>% to_duckdb() |>
group_by(p.ID, LAD_origin, scenario)%>%
summarise(Cycling=sum(t.distance_bike[mode=="Cycling"]),
Walking=sum(t.distance_walk[mode=="Walking"]),
`Public Transport`=sum(t.distance_auto[mode=="Public Transport"]),
`Driving Car`=sum(t.distance_auto[mode=="Driving Car"]),
`Car Passenger`=sum(t.distance_auto[mode=="Car Passenger"])) |> collect()
pp=pp%>%gather(mode,dist,Cycling:`Car Passenger`)
pop_lad <- trips |> to_duckdb() |> distinct(p.ID, LAD_origin) |> group_by(LAD_origin) |> summarise(pop = n())
tot_pop <- pop_lad |> dplyr::select(pop) |> pull() |> sum()
summary_distance <- pp |>
filter(!is.na(dist)) |>
left_join(pop_lad, copy = T) |>
group_by(mode, LAD_origin, scenario) |>
summarise(avgDistance = round(sum(dist) / first(pop), 1))
pp_all=trips%>% to_duckdb() |>
group_by(p.ID, scenario)%>%
summarise(Cycling=sum(t.distance_bike[mode=="Cycling"]),
Walking=sum(t.distance_walk[mode=="Walking"]),
`Public Transport`=sum(t.distance_auto[mode=="Public Transport"]),
`Driving Car`=sum(t.distance_auto[mode=="Driving Car"]),
`Car Passenger`=sum(t.distance_auto[mode=="Car Passenger"])) |> collect()
pp_all <- pp_all%>%gather(mode,dist,Cycling:`Car Passenger`)
summary_distance_all <- pp_all |>
filter(!is.na(dist)) |>
group_by(mode, scenario) |>
summarise(avgDistance = round(sum(dist, na.rm = T) / tot_pop, 1)) |>
mutate(LAD_origin = "All Locations")
combined_distance <- bind_rows(summary_distance,summary_distance_all)
ggplotly(
ggplot(combined_distance) +
aes(x = mode, y = avgDistance, fill = scenario) +
geom_col(position = "dodge2") +
scale_fill_hue(direction = 1) +
geom_text(aes(label = round(avgDistance, 1), y = avgDistance),
size = 2, #hjust = -0.1,
hjust = 1.1,
vjust = 0.2,
position = position_dodge(1),
inherit.aes = TRUE
) +
coord_flip() +
theme_minimal() +
facet_wrap(vars(LAD_origin)) +
labs(title = "Average weekly dist. pp by mode and location",
fill = "Scenario")
)
```
# Average time spent per person by mode and location
```{r}
tt= trips %>% to_duckdb() |>
group_by(p.ID, LAD_origin, scenario)%>%
summarise(Cycling=sum(time_bike[mode=="Cycling"], na.rm = T),
Walking=sum(time_walk[mode=="Walking"], na.rm = T),
`Public Transport`=sum(time_pt[mode=="Public Transport"], na.rm = T),
`Driving Car`=sum(time_auto[mode=="Driving Car"], na.rm = T),
`Car Passenger`=sum(time_auto[mode=="Car Passenger"], na.rm = T)) |> collect()
tt=tt%>%gather(mode,time,Cycling:`Car Passenger`)
summary_time=tt%>%
group_by(mode, LAD_origin, scenario)%>%
summarise(avgTime=mean(time, na.rm = T))
tt_all=trips%>% to_duckdb() |>
group_by(p.ID, scenario)%>%
summarise(Cycling=sum(time_bike[mode=="Cycling"], na.rm = T),
Walking=sum(time_walk[mode=="Walking"], na.rm = T),
`Public Transport`=sum(time_pt[mode=="Public Transport"], na.rm = T),
`Driving Car`=sum(time_auto[mode=="Driving Car"], na.rm = T),
`Car Passenger`=sum(time_auto[mode=="Car Passenger"], na.rm = T)) |> collect()
tt_all=tt_all%>%gather(mode,time,Cycling:`Car Passenger`)
summary_time_all=tt_all%>%
group_by(mode, scenario)%>%
summarise(avgTime=mean(time, na.rm = T)) %>%
mutate(LAD_origin = "All Locations")
avg_time_combined <- bind_rows(summary_time, summary_time_all)
ggplotly(ggplot(avg_time_combined) +
aes(x = mode, y = avgTime, fill = scenario) +
geom_col(position = "dodge2") +
geom_text(aes(label = round(avgTime, 1),
y = avgTime),
size = 2, #hjust = -0.1,
hjust = 1.1,
vjust = 0.2,
position = position_dodge(1),
inherit.aes = TRUE
) +
scale_fill_hue(direction = 1) +
labs(title = "Average weekly time (in hours) by mode per person and location",
fill = "Scenario",
x = "", y = "Hours") +
coord_flip() +
theme_minimal() +
facet_wrap(vars(LAD_origin))
)
# Stacked Bar Plots for Average Distance via Transport Mode
trips <- trips %>%
mutate(distance = case_when(
mode %in% c("Driving Car", "Car Passenger", "Public Transport") ~ t.distance_auto,
mode == "Walking" ~ t.distance_walk,
mode == "Cycling" ~ t.distance_bike,
TRUE ~ NA_real_))
trips$distance_bracket <- cut(
trips$distance,
breaks = c(0, 1, 3, 5, 10, 20, 40, Inf),
labels = c("0-1", "1-3", "3-5", "5-10", "10-20", "20-40", "40+"),
right = FALSE)
distance <- trips %>%
group_by(distance_bracket, mode, scenario) %>%
summarise(count = n(), .groups = 'drop') %>%
group_by(distance_bracket, scenario) %>%
mutate(percent = count / sum(count) * 100)
ggplot(distance, aes(x = distance_bracket, y = percent, fill = mode)) +
geom_bar(stat = "identity", position = "fill") +
geom_text(
aes(label = ifelse(percent > 1, paste0(round(percent, 1), "%"), "")), # Show label only if >= 1%
position = position_fill(vjust = 0.5),
color = "white",
size = 3
) +
labs(
title = "Transport Mode Share by Trip Distance",
y = "Proportion (%)",
x = "Distance (km)",
fill = "Transport Mode"
) +
theme_minimal(base_size = 12) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.y = element_blank(),
axis.text.x = element_text(face = "bold"),
strip.placement = "outside",
strip.text = element_text(face = "bold"),
legend.text = element_text(face = "bold"),
legend.title = element_text(face = "bold")
) +
facet_wrap(~ scenario, scales = "free_x")
# People with Zero trips via mode
zero_mode <- trips %>%
group_by(scenario) %>%
summarise(total = n_distinct(p.ID),
.groups = 'drop') %>%
right_join(
trips %>%
group_by(mode, scenario) %>%
summarise(count = n_distinct(p.ID), .groups = 'drop'),
by = "scenario") %>%
mutate(zero = total - count,
zero_percent = round(zero/total*100,1))
ggplot(zero_mode, aes(x = mode, y = zero_percent, fill = scenario)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
geom_text(
aes(label = paste0(zero_percent, "%")),
position = position_dodge(width = 0.9),
vjust = -0.25,
size = 3) +
labs(
title = "Proportion of Individuals Reporting Non-Usage of Specific Transport Modes",
y = "Proportion (%)",
fill = "Scenario") +
theme_minimal() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.x = element_text(face = "bold"),
axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold"),
axis.text.y = element_text(face = "bold"),
legend.text = element_text(face = "bold"),
legend.title = element_text(face = "bold"))
```