-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.R
More file actions
510 lines (459 loc) · 20.1 KB
/
script.R
File metadata and controls
510 lines (459 loc) · 20.1 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
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
################################################################################
# Load libraries
################################################################################
library(tidyverse)
library(sf)
library(cmapgeo)
library(cmapplot)
################################################################################
# Load MDT data from separate repo
################################################################################
source("../mydailytravel/R/data_cleaning.R")
source("../mydailytravel/R/helper_fns.R")
setwd("../task3_mr")
################################################################################
# Prep data
################################################################################
# Identify which trips end in the central area (zones 1-77)
central_area <-
cmapgeo::zone_sf %>%
filter(zone17 <= 77)
tracts_in_central_area <-
cmapgeo::tract_sf[lengths(st_intersects(tract_sf,central_area))!=0,] %>%
select(geoid_tract) %>%
# Manually remove three that are only bordering the central area
filter(geoid_tract != "17031310800",
geoid_tract != "17031071500",
geoid_tract != "17031841000") %>%
mutate(state_fips = substr(geoid_tract,1,2),
county_fips = substr(geoid_tract,3,5),
tract_fips = substr(geoid_tract,6,11)) %>%
mutate(tract_fips_n = as.numeric(tract_fips))
st_geometry(tracts_in_central_area) <- NULL
# Export
write.csv(tracts_in_central_area,"tracts_in_central_area.csv")
# Create helper value to allow for collapsing of distinct work trip chains
chain_id <-
# Load chain data from MDT
chains %>%
# Arrange to allow for identification of separate work chains
arrange(sampno,perno,placeno) %>%
# Group by sample and person to identify individuals
group_by(sampno,perno) %>%
# Identify distinct sets of trips, varying by whether they are home_to_work or
# not. Note that this will not vary between other categories of work chain
# trips, since we are only trying to capture journeys from home to work, and
# not work to work or work to home.
mutate(chain_id = cumsum(c(1,diff(home_to_work) != 0))) %>%
ungroup()
# Add back chain information
mdt_chains <-
mdt %>%
left_join(chain_id %>% select(sampno,perno,placeno,
home_to_work,work_to_work,work_to_home,
chain_id),
by = c("sampno","perno","placeno"))
# Create base data
mode_share_base <-
mdt_chains %>% # 125463 records
# Exclude "beginning" trips
filter(mode_c != "beginning") %>% # 97374
# Exclude trips with no travel distance.
filter(distance_pg > 0) %>% # 97316
# Exclude trips with a "missing" mode
filter(mode_c != "missing") %>% # 97279
# Add central area flag to 'county_chi_name'
mutate(destination_place = case_when(
# Flag all tracts in the central area
state_fips == 17 &
county_fips == 31 &
tract_fips %in% tracts_in_central_area$tract_fips_n ~ "Central area",
# For Chicago locations that are outside the CBD, mark them appropriately
county_chi_name == "Chicago" ~ "Chicago (excl. CBD)",
# All other locations will be marked with the original county location,
# which included a "Suburban Cook" designation.
TRUE ~ county_chi_name
)) %>%
# Exclude NA destinations
filter(!is.na(destination_place)) %>% # 96907
# Remove destinations in Grundy and DeKalb
filter(!(destination_place %in% c("Grundy","DeKalb"))) %>% # 95169
# Add factor ordering
mutate(destination_place = factor(destination_place)) %>%
# Keep relevant variables
select(sampno,perno,weight,placeno,
mode,mode_c,tpurp,tpurp_c,travtime_pg_calc,distance_pg,
home_to_work,chain_id,chain_c,
home_county_chi,
destination_place,county_chi_name,county_chi_name_lag)
################################################################################
# Analyze destination mode share
################################################################################
destination_alltrips_mode_share <-
pct_calculator(mode_share_base,
breakdown_by = "mode_c",
second_breakdown = "destination_place",
weight = "weight")
destination_worktrips_mode_share <-
pct_calculator(mode_share_base %>%
# Keep only trips to work
filter(home_to_work == 1) %>%
# Keep the longest leg of a chain, as that is likeliest to be
# the relevant travel component
group_by(sampno,perno,chain_id) %>%
arrange(desc(distance_pg)) %>%
distinct(sampno,perno,chain_id,.keep_all = TRUE) %>%
ungroup(),
breakdown_by = "mode_c",
second_breakdown = "destination_place",
weight = "weight")
################################################################################
# Destination mode share exports
################################################################################
# Export all trips mode share
t1 <-
destination_alltrips_mode_share %>%
select(mode = mode_c,
destination = destination_place,
trips = total,
pct = pct,
n = total_n) %>%
mutate(pct = round(pct, 4)) %>%
pivot_wider(names_from = mode,values_from = pct)
write.csv(t1,"destination_alltrips_mode_share.csv")
# Export work trips mode share
t2 <-
destination_worktrips_mode_share %>%
select(mode = mode_c,
destination = destination_place,
trips = total,
pct = pct,
n = total_n) %>%
mutate(pct = round(pct, 4)) %>%
pivot_wider(names_from = mode,values_from = pct)
write.csv(t2,"destination_worktrips_mode_share.csv")
# Create labels for all trips mode share chart
p1_labels <-
destination_alltrips_mode_share %>%
filter(mode_c %in% c("walk","transit","bike","schoolbus","other")) %>%
group_by(destination_place) %>%
summarize(label = sum(pct))
# Create plot
p1 <-
# Get data
destination_alltrips_mode_share %>%
# Add labels
left_join(p1_labels,
by = c("destination_place")) %>%
# Make changes for graphing
mutate(
# Reorder factors and capitalize
mode_c = recode_factor(factor(mode_c,levels =
c("driver","passenger","walk",
"transit","bike","schoolbus","other")),
"passenger" = "Passenger",
"driver" = "Driver",
"walk" = "Walk",
"transit" = "Transit",
"bike" = "Bike",
"schoolbus" = "School bus",
"other" = "Other"),
# Make driver/passenger go on the left-hand-side of the graph
pct = ifelse(mode_c %in% c("Driver","Passenger"),-1 *pct,pct)) %>%
# Create ggplot object
ggplot(aes(x = pct, y = reorder(destination_place,label))) +
geom_col(aes(fill = mode_c),
position = position_stack(reverse = T)) +
# Add CMAP style
theme_cmap(gridlines = "v", vline = 0,
xlab = "Mode share by destination",
strip.text = element_text(family = "Whitney Semibold",
hjust = 0.5)) +
# Add colors
scale_fill_manual(values = c("#e5bd72","#8c0000","#36d8ca","#6d8692","#efa7a7","#3d6600","#0084ac"),
labels = c("Driver","Passenger","Walk","Transit","Bike","School bus","Other")) +
# Adjust axis
scale_x_continuous(breaks = seq(-1,.75,by = .25),
labels = scales::label_percent()(abs(seq(-1,.75,by = .25))),
limits = c(-1,.75),
expand = expansion(mult = c(.05,0))
) +
# Adjust legend for formatting
guides(fill = guide_legend(ncol = 7,
override.aes = list(fill = c("#8c0000","#e5bd72","#36d8ca",
"#6d8692","#efa7a7","#3d6600",
"#0084ac"))))
# Export finalized graphic
finalize_plot(p1,
title = "Mode share by destination, all trips (2019).",
caption = "Note: The Central Area includes all trips to
tracts within the area bounded by North Ave., Ashland Ave.,
Cermak Rd., and Lake Michigan.
<br><br>
Source: Chicago Metropolitan Agency for Planning analysis of My
Daily Travel data.",
filename = "p1",
height = 4,
mode = c("png","pdf"),
overwrite = T)
# Create labels
p2_labels <-
destination_worktrips_mode_share %>%
filter(mode_c %in% c("walk","transit","bike","schoolbus","other")) %>%
group_by(destination_place) %>%
summarize(label = sum(pct))
# Create plot
p2 <-
# Get data
destination_worktrips_mode_share %>%
# Add labels
left_join(p2_labels,
by = c("destination_place")) %>%
# Make changes for graphing
mutate(
# Reorder factors and capitalize
mode_c = recode_factor(factor(mode_c,levels =
c("driver","passenger","walk",
"transit","bike","schoolbus","other")),
"passenger" = "Passenger",
"driver" = "Driver",
"walk" = "Walk",
"transit" = "Transit",
"bike" = "Bike",
"schoolbus" = "School bus",
"other" = "Other"),
# Make driver/passenger go on the left-hand-side of the graph
pct = ifelse(mode_c %in% c("Driver","Passenger"),-1 *pct,pct)) %>%
# Create ggplot object
ggplot(aes(x = pct, y = reorder(destination_place,label))) +
geom_col(aes(fill = mode_c),
position = position_stack(reverse = T)) +
# Add CMAP style
theme_cmap(gridlines = "v", vline = 0,
xlab = "Mode share by destination",
strip.text = element_text(family = "Whitney Semibold",
hjust = 0.5)) +
# Add colors
scale_fill_manual(values = c("#e5bd72","#8c0000","#36d8ca","#6d8692","#efa7a7","#3d6600","#0084ac"),
labels = c("Driver","Passenger","Walk","Transit","Bike","School bus","Other")) +
# Adjust axis
scale_x_continuous(breaks = seq(-1,.75,by = .25),
labels = scales::label_percent()(abs(seq(-1,.75,by = .25))),
limits = c(-1,.75),
expand = expansion(mult = c(.05,0))
) +
# Adjust legend for formatting
guides(fill = guide_legend(ncol = 7,
override.aes = list(fill = c("#8c0000","#e5bd72","#36d8ca",
"#6d8692","#efa7a7","#3d6600",
"#0084ac"))))
# Export finalized graphic
finalize_plot(p2,
title = "Mode share by destination, trips to work (2019).",
caption = "Note: The Central Area includes all trips to
tracts within the area bounded by North Ave., Ashland Ave.,
Cermak Rd., and Lake Michigan. Includes only work trips identified
as part of a journey from home to work. For work chains with
multiple legs, only includes the segment with the longest travel
distance.
<br><br>
Source: Chicago Metropolitan Agency for Planning analysis of My
Daily Travel data.",
filename = "p2",
height = 4,
mode = c("png","pdf"),
overwrite = T)
################################################################################
# Travel statistics
################################################################################
# Travel statistics by destination
destination_worktrip_travel_stats <-
mode_share_base %>%
# Keep only trips from home to work
filter(home_to_work == 1) %>%
# Group by person and trip chain ID
group_by(sampno,perno,chain_id) %>%
# Create summaries of entire chains
arrange(sampno,perno,placeno) %>%
summarize(distance_pg = sum(distance_pg),
travtime_pg_calc = sum(travtime_pg_calc),
weight = median(weight),
destination_place = last(destination_place),
legs = n()) %>%
ungroup() %>%
# Create flag for multi-leg journeys
mutate(multi_leg = ifelse(legs == 1,"Single","Multi")) %>%
group_by(destination_place
# ,multi_leg
) %>%
summarize(distance = weighted.mean(distance_pg,wt = weight),
time = weighted.mean(travtime_pg_calc, wt = weight),
n = n())
# Travel statistics by destination for non-work trips
destination_nonworktrip_travel_stats <-
mode_share_base %>%
# Exclude all trips connected to a work trip chain. Note this excludes more
# than just those analyzed above, which were only home to work - this also
# excludes work to work and work to home to provide a picture of non-work
# travel.
filter(chain_c != "work") %>%
group_by(destination_place) %>%
summarize(distance = weighted.mean(distance_pg,wt = weight),
time = weighted.mean(travtime_pg_calc, wt = weight),
n = n())
# Travel statistics by destination for all trips
destination_alltrip_travel_stats <-
mode_share_base %>%
group_by(destination_place) %>%
summarize(distance = weighted.mean(distance_pg,wt = weight),
time = weighted.mean(travtime_pg_calc, wt = weight),
n = n())
# Export travel stats
write.csv(destination_worktrip_travel_stats,"destination_worktrip_travel_stats.csv")
write.csv(destination_alltrip_travel_stats,"destination_alltrip_travel_stats.csv")
write.csv(destination_nonworktrip_travel_stats,"destination_nonworktrip_travel_stats.csv")
# # Chart of travel statistics
# p3 <-
# destination_travel_stats %>%
# mutate(destination_place = factor(destination_place,
# levels = rev(levels(destination_travel_stats$destination_place)))) %>%
# ggplot(aes(x = distance,
# y = destination_place)) +
# geom_col(aes(fill = flag),
# position = position_dodge2(reverse = T)) +
# theme_cmap(vline = 0, gridlines = "v",
# xlab = "Distance traveled (miles)") +
# cmap_fill_discrete("friday")
#
# finalize_plot(p3,
# "Distance traveled by destination, journeys to work vs. all trips (2019).",
# "Note: 'Trips to work' includes all trips recorded as
# part of a trip chain between a survey respondent's home location
# and their work location. The Central Area includes all trips to
# tracts within the area bounded by North Ave., Ashland Ave.,
# Cermak Rd., and Lake Michigan.
# <br><br>
# Source: Chicago Metropolitan Agency for Planning analysis of My
# Daily Travel data.",
# filename = "p3",
# # height = 4,
# # mode = c("png","pdf"),
# overwrite = T)
#
# p4 <-
# destination_travel_stats %>%
# mutate(destination_place = factor(destination_place,
# levels = rev(levels(destination_travel_stats$destination_place)))) %>%
# ggplot(aes(x = time,
# y = destination_place)) +
# geom_col(aes(fill = flag),
# position = position_dodge2(reverse = T)) +
# theme_cmap(vline = 0, gridlines = "v",
# xlab = "Trip duration (minutes)")+
# cmap_fill_discrete("friday")
#
# finalize_plot(p4,
# "Travel time by destination, journeys to work vs. all trips (2019).",
# "Note: 'Trips to work' includes all trips recorded as
# part of a trip chain between a survey respondent's home location
# and their work location. The Central Area includes all trips to
# tracts within the area bounded by North Ave., Ashland Ave.,
# Cermak Rd., and Lake Michigan.
# <br><br>
# Source: Chicago Metropolitan Agency for Planning analysis of My
# Daily Travel data.",
# filename = "p4",
# # height = 4,
# # mode = c("png","pdf"),
# overwrite = T)
# # Summary of travel into the CBD
#
# cbd_mode_share <-
# pct_calculator(mode_share_base %>%
# mutate(flag = "All trips") %>%
# filter(
# destination_place == "Central area",
# !(county_chi_name_lag %in% c("Grundy","DeKalb")),
# !is.na(county_chi_name_lag)) %>%
# rbind(mode_share_base %>%
# filter(home_to_work == 1,
# destination_place == "Central area",
# !(county_chi_name_lag %in% c("Grundy","DeKalb")),
# !is.na(county_chi_name_lag)) %>%
# mutate(flag = "Journey to work")),
# breakdown_by = "mode_c",
# second_breakdown = "county_chi_name_lag",
# third_breakdown = "flag",
# weight = "weight") %>%
# # mutate(pct = round(pct,2)) %>%
# arrange(county_chi_name_lag) # %>% View()
#
# # Create labels
# p5_labels <-
# cbd_mode_share %>%
# filter(mode_c %in% c("walk","transit","bike","schoolbus","other")) %>%
# group_by(county_chi_name_lag,flag) %>%
# summarize(label = sum(pct))
#
# # Create plot
# p5 <-
# # Get data
# cbd_mode_share %>%
# # Add labels
# left_join(p5_labels,
# by = c("county_chi_name_lag",
# "flag")) %>%
# # Make changes for graphing
# mutate(
# # Reorder factors and capitalize
# mode_c = recode_factor(factor(mode_c,levels =
# c("driver","passenger","walk",
# "transit","bike","schoolbus","other")),
# "passenger" = "Passenger",
# "driver" = "Driver",
# "walk" = "Walk",
# "transit" = "Transit",
# "bike" = "Bike",
# "schoolbus" = "School bus",
# "other" = "Other"),
# # Make driver/passenger go on the left-hand-side of the graph
# pct = ifelse(mode_c %in% c("Driver","Passenger"),-1 *pct,pct)) %>%
#
# # Create ggplot object
# ggplot(aes(x = pct, y = reorder(county_chi_name_lag,label))) +
# geom_col(aes(fill = mode_c),
# position = position_stack(reverse = T)) +
#
# # Add CMAP style
# theme_cmap(gridlines = "v", vline = 0,
# xlab = "Mode share") +
# # Add colors
# scale_fill_manual(values = c("#e5bd72","#8c0000","#36d8ca","#6d8692","#efa7a7","#3d6600","#0084ac"),
# labels = c("Driver","Passenger","Walk","Transit","Bike","School bus","Other")) +
#
# # Adjust axis
# scale_x_continuous(breaks = seq(-.75,1,by = .25),
# labels = scales::label_percent()(abs(seq(-.75,1,by = .25))),
# limits = c(-.75,1),
# expand = expansion(mult = c(.05,0))
# ) +
#
# # Adjust legend for formatting
# guides(fill = guide_legend(ncol = 7,
# override.aes = list(fill = c("#8c0000","#e5bd72","#36d8ca",
# "#6d8692","#efa7a7","#3d6600",
# "#0084ac")))) +
# facet_wrap(~flag,ncol = 1)
#
# # Export finalized graphic
# finalize_plot(p5,
# title = "Mode share into the CBD by home location, trips to and from work vs. other trips (2019).",
# caption = "Note: The Central Area includes all trips to tracts
# within the area bounded by North Ave., Ashland Ave., Cermak Rd.,
# and Lake Michigan.
# <br><br>
# Source: Chicago Metropolitan Agency for Planning analysis of My Daily Travel data.",
# filename = "p5",
# # height = 4,
# # mode = c("png","pdf"),
# overwrite = T)