-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cyclistic Data Cleaning and Analysis.R
683 lines (501 loc) · 34.1 KB
/
Cyclistic Data Cleaning and Analysis.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
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
# DATA IMPORTING
# Load libraries necessary for all steps in the analysis proccess of Cyclistic's historical data
library(tidyverse)
library(lubridate)
library(geosphere)
library(ggmap)
# Import 12 most recent monthly datasets (August 2022 to July 2023) into RStudio using read.csv function
CyclisticData202307 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202307-divvy-tripdata.csv")
CyclisticData202306 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202306-divvy-tripdata.csv")
CyclisticData202305 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202305-divvy-tripdata.csv")
CyclisticData202304 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202304-divvy-tripdata.csv")
CyclisticData202303 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202303-divvy-tripdata.csv")
CyclisticData202302 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202302-divvy-tripdata.csv")
CyclisticData202301 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202301-divvy-tripdata.csv")
CyclisticData202212 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202212-divvy-tripdata.csv")
CyclisticData202211 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202211-divvy-tripdata.csv")
CyclisticData202210 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202210-divvy-tripdata.csv")
CyclisticData202209 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202209-divvy-publictripdata.csv")
CyclisticData202208 <- read_csv("/kaggle/input/divvy-trip-data-august-2022-july-2023/202208-divvy-tripdata.csv")
# Merge these 12 together into a new, single data frame
CyclisticMerged <- rbind(CyclisticData202307, CyclisticData202306, CyclisticData202305,
CyclisticData202304, CyclisticData202303, CyclisticData202302,
CyclisticData202301, CyclisticData202212, CyclisticData202211,
CyclisticData202210, CyclisticData202209, CyclisticData202208)
# View and explore merged data frame of 1 year of historical data of Cyclistic rides
head(CyclisticMerged)
glimpse(CyclisticMerged)
summary(CyclisticMerged)
# DATA CLEANING AND WRANGLING
# ride_id
# Cleaning - Remove duplicates to ensure each ride has one unique, corresponding observation/row
CyclisticMergedNoDup <- CyclisticMerged %>%
distinct(ride_id, .keep_all = TRUE)
print(paste("Removed", nrow(CyclisticMerged) - nrow(CyclisticMergedNoDup),
"duplicate rows corresponding to ride_id"))
# Cleaning - check for and remove any missing values (NA)
print(paste("ride_id has", sum(is.na(CyclisticMergedNoDup$ride_id)), "missing values" ))
# rideable_type
# Transformation - convert data type from character to factor for an easier analysis later on
CyclisticMergedNoDup$rideable_type <- as.factor(CyclisticMergedNoDup$rideable_type)
class(CyclisticMergedNoDup$rideable_type)
nlevels(CyclisticMergedNoDup$rideable_type)
summary(CyclisticMergedNoDup$rideable_type)
# Change values of "docked_bike" to "classic_bike"
CyclisticMergedNoDup$rideable_type[CyclisticMergedNoDup$rideable_type == "docked_bike"] <- "classic_bike"
summary(CyclisticMergedNoDup$rideable_type)
# started_at, ended_at
# Transformation - create new column of calculated ride time by using the difftime function to find the difference
# between end time and start time of a ride
CyclisticMergedNoDup$ride_time_minutes <- (as.double(difftime(CyclisticMerged$ended_at, CyclisticMerged$started_at))) /60
summary(CyclisticMergedNoDup$ride_time_minutes)
# Cleaning - removing outliers
# Seperate ride_time_minutes into quantiles to see how ride time is distrubuted and then how to filter values
quantile_ride_time = quantile(CyclisticMergedNoDup$ride_time_minutes, seq(0, 1, by=0.01))
quantile_ride_time
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
filter(ride_time_minutes >= 1) %>% # negative ride times and rides times under 60 seconds
filter(ride_time_minutes <= 180) # 3 hours, exlude large ride times to not skew mean
summary(CyclisticMergedNoDup$ride_time_minutes)
# Parsing - Extracting the month, weekday, and hour of each ride from started_at into their own respective variables,
# using strftime and floor_date functions from lubridate package
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
mutate(month = strftime(CyclisticMergedNoDup$started_at, "%B"),
weekday = strftime(CyclisticMergedNoDup$started_at, "%A"))
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
mutate(hour_started = floor_date(started_at, unit = "hour"))
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
mutate(hour_started = strftime(CyclisticMergedNoDup$hour_started,"%R"))
head(CyclisticMergedNoDup)
# Transformation - Change data type of new columns from character to factor in order for an easier analysis later on
CyclisticMergedNoDup$month <- as.factor(CyclisticMergedNoDup$month)
CyclisticMergedNoDup$weekday <- as.factor(CyclisticMergedNoDup$weekday)
CyclisticMergedNoDup$hour_started <- as.factor(CyclisticMergedNoDup$hour_started)
class(CyclisticMergedNoDup$month)
nlevels(CyclisticMergedNoDup$month)
summary(CyclisticMergedNoDup$month)
class(CyclisticMergedNoDup$weekday)
nlevels(CyclisticMergedNoDup$weekday)
summary(CyclisticMergedNoDup$weekday)
class(CyclisticMergedNoDup$hour_started)
nlevels(CyclisticMergedNoDup$hour_started)
summary(CyclisticMergedNoDup$hour_started)
# start_station_name, end_station_name, start_station_id, end_station_id
# Transformation - Change data types of columns from character to factor
CyclisticMergedNoDup$start_station_name <- as.factor(CyclisticMergedNoDup$start_station_name)
CyclisticMergedNoDup$end_station_name <- as.factor(CyclisticMergedNoDup$end_station_name)
CyclisticMergedNoDup$start_station_id <- as.factor(CyclisticMergedNoDup$start_station_id)
CyclisticMergedNoDup$end_station_id <- as.factor(CyclisticMergedNoDup$end_station_id)
class(CyclisticMergedNoDup$start_station_name)
nlevels(CyclisticMergedNoDup$start_station_name)
class(CyclisticMergedNoDup$end_station_name)
nlevels(CyclisticMergedNoDup$end_station_name)
class(CyclisticMergedNoDup$start_station_id)
nlevels(CyclisticMergedNoDup$start_station_id)
class(CyclisticMergedNoDup$end_station_id)
nlevels(CyclisticMergedNoDup$end_station_id)
# start_lat, start_lng, end_lat, end_lng
# Transformation - Create column of calculated distance between start and end points with the coordinate values
# contained in these 4 variables, use distGeo function
CyclisticMergedNoDup$distance_between_stations_km <-
distGeo(matrix(c(CyclisticMergedNoDup$start_lng,CyclisticMergedNoDup$start_lat), ncol = 2),
matrix(c(CyclisticMergedNoDup$end_lng,CyclisticMergedNoDup$end_lat), ncol = 2)) / 1000
summary(CyclisticMergedNoDup$distance_between_stations_km)
# Cleaning - outliers, NA's
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
arrange(desc(distance_between_stations_km))
head(CyclisticMergedNoDup$distance_between_stations_km, n = 10)
# After arranging the data frame by distance, sorted from largest to smallest values, 8 outliers with illogical
# values have been identified to be removed
CyclisticMergedNoDup <- CyclisticMergedNoDup %>%
slice(-(1:8))
head(CyclisticMergedNoDup$distance_between_stations_km, n = 10)
summary(CyclisticMergedNoDup$distance_between_stations_km)
# member_casual
# Transformation - Change data type from character to factor
CyclisticMergedNoDup$member_casual <- as.factor(CyclisticMergedNoDup$member_casual)
class(CyclisticMergedNoDup$member_casual)
nlevels(CyclisticMergedNoDup$member_casual)
summary(CyclisticMergedNoDup$member_casual)
# Final Check Before Analysis
# Look through the summary of cleaned data frame before performing analysis
# Check missing values for each variable, explore further, decide to keep/remove/replace and when
summary(CyclisticMergedNoDup)
print(paste("ride_id has", sum(is.na(CyclisticMergedNoDup$ride_id)), "missing values"))
print(paste("rideable_type has", sum(is.na(CyclisticMergedNoDup$rideable_type)), "missing values"))
print(paste("started_at has", sum(is.na(CyclisticMergedNoDup$started_at)), "missing values"))
print(paste("ended_at has", sum(is.na(CyclisticMergedNoDup$ended_at)),"missing values"))
print(paste("start_station_name has", sum(is.na(CyclisticMergedNoDup$start_station_name)),"missing values"))
print(paste("start_station_id has", sum(is.na(CyclisticMergedNoDup$start_station_id)),"missing values"))
print(paste("end_station_name has", sum(is.na(CyclisticMergedNoDup$end_station_name)),"missing values"))
print(paste("end_station_id has", sum(is.na(CyclisticMergedNoDup$end_station_id)),"missing values"))
print(paste("start_lat has", sum(is.na(CyclisticMergedNoDup$start_lat)),"missing values"))
print(paste("start_lng has", sum(is.na(CyclisticMergedNoDup$start_lng)),"missing values"))
print(paste("end_lat has", sum(is.na(CyclisticMergedNoDup$end_lat)),"missing values"))
print(paste("end_lng has", sum(is.na(CyclisticMergedNoDup$end_lng)),"missing values"))
print(paste("member_casual has", sum(is.na(CyclisticMergedNoDup$member_casual)),"missing values"))
print(paste("ride_time_minutes has", sum(is.na(CyclisticMergedNoDup$ride_time_minutes)),"missing values"))
print(paste("month has", sum(is.na(CyclisticMergedNoDup$month)),"missing values"))
print(paste("weekday has", sum(is.na(CyclisticMergedNoDup$weekday)),"missing values"))
print(paste("hour_started has", sum(is.na(CyclisticMergedNoDup$hour_started)),"missing values"))
print(paste("distance_between_stations_km has", sum(is.na(CyclisticMergedNoDup$distance_between_stations_km)),"missing values"))
# Variables containing station data have significant amount of missing values.
# Subset into seperate data set to explore further.
NAinSSN <- subset(CyclisticMergedNoDup, is.na(CyclisticMergedNoDup$start_station_name))
NAinSSI <- subset(CyclisticMergedNoDup, is.na(CyclisticMergedNoDup$start_station_id))
NAinESN <- subset(CyclisticMergedNoDup, is.na(CyclisticMergedNoDup$end_station_name))
NAinESI <- subset(CyclisticMergedNoDup, is.na(CyclisticMergedNoDup$end_station_id))
head(NAinSSN)
head(NAinSSI)
head(NAinESN)
head(NAinESI)
summary(NAinSSN$rideable_type)
summary(NAinSSI$rideable_type)
summary(NAinESN$rideable_type)
summary(NAinESI$rideable_type)
# Drop columns that will not be used in your analysis and visualization
CyclisticClean <- CyclisticMergedNoDup %>%
select(-c(started_at, ended_at))
head(CyclisticClean)
summary(CyclisticClean)
# DATA ANALYSIS AND VISUALIZATION
# summarise the data by the number of rides taken by each User type to see how they compare.
member_casual_summary <- CyclisticClean %>%
group_by(member_casual) %>%
summarise(count = n(), percentage = round(length(ride_id)/nrow(CyclisticClean)*100,2),
.groups = "drop")
options(scipen = 999, repr.plot.width = 11, repr.plot.height = 8)
ggplot(member_casual_summary, aes(x = " " , y = count, fill = member_casual)) +
geom_col() + labs(title = "Number of Rides Taken by User Type (Aug 2022 - July 2023)") +
geom_text(aes(label = paste(count," --- ", percentage ,"%")), position = position_stack(vjust = 0.5),
size = 6) + theme_void(base_size = 19) + scale_fill_discrete(name = "User Type") + coord_polar(theta = "y")
# look into the differences in ride count by grouping the counts by the Month that the rides took place
CyclisticClean$month <- ordered(CyclisticClean$month, levels =
c("August", "September", "October", "November", "December", "January",
"February", "March", "April", "May", "June", "July"))
month_member_casual_summary <- CyclisticClean %>%
group_by(month, member_casual) %>%
summarise(count = n(), .groups = "drop")%>%
arrange(month, member_casual)
options(repr.plot.width = 16, repr.plot.height = 8)
ggplot(data = month_member_casual_summary, aes(x = month, y = count, fill = member_casual),
show.legend = FALSE) + geom_col(position = "dodge", width = 0.6) +
labs(title = "Monthly Ride Count by User Type (Aug 2022 - July 2023)", x = "Month", y = "# of Rides") +
scale_fill_discrete(name="User Type") + theme_gray(base_size = 19)
# group ride counts by the days of the week
CyclisticClean$weekday <- ordered(CyclisticClean$weekday, levels =
c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"))
weekday_member_casual_summary <- CyclisticClean %>%
group_by(weekday, member_casual) %>%
summarise(count = n(), .groups = "drop")%>%
arrange(weekday, member_casual)
ggplot(data = weekday_member_casual_summary, aes(x = weekday, y = count, fill = member_casual),
show.legend = FALSE) + geom_col(position = "dodge", width = 0.8) +
labs(title = "Ride Count by Day of the Week (Aug 2022 - July 2023)", x = "Day", y = "# of Rides") +
scale_fill_discrete(name="User Type") + theme_gray(base_size = 19) + geom_text(aes(label = count),
size = 5, vjust = -0.25, position = position_dodge(width = 0.8))
# time of day these rides are taking place for both the workweek and weekend
hour_member_casual_summary <- CyclisticClean %>%
filter(weekday == "Monday" | weekday == "Tuesday"| weekday == "Wednesday" |
weekday == "Thursday" |weekday == "Friday") %>%
group_by(hour_started, member_casual) %>%
summarise(count = n(), .groups = "drop")%>%
arrange(hour_started, member_casual)
ggplot(data = hour_member_casual_summary, aes(x = hour_started, y = count, group = member_casual,
color = member_casual)) + geom_point() + geom_line() +
labs(title = "Ride Count by Time of Day on Weekdays (Aug 2022 - July 2023)", x = "Hour", y = "# of Rides") +
guides(color = guide_legend(title = "User Type")) +
theme(text = element_text(size = 19),axis.text.x = element_text(angle = 90))
hour_member_casual_summary1 <- CyclisticClean %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(hour_started, member_casual) %>%
summarise(count = n(), .groups = "drop") %>%
arrange(hour_started, member_casual)
ggplot(data = hour_member_casual_summary1, aes(x = hour_started, y = count, group = member_casual,
color = member_casual)) + geom_point() + geom_line() +
labs(title = "Ride Count by Time of Day on Weekends (Aug 2022 - July 2023)", x = "Hour",
y = "# of Rides") + guides(color = guide_legend(title = "User Type")) +
theme(text = element_text(size = 19),axis.text.x = element_text(angle = 90))
# ride time of our users
ride_time_summary <- CyclisticClean %>%
group_by(member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),1), median = round(median(ride_time_minutes),1), .groups = "drop")
options(repr.plot.width = 10, repr.plot.height = 8)
ggplot(data = ride_time_summary) +
geom_col(mapping = aes(x = member_casual , y = mean, fill = member_casual), show.legend = FALSE) +
labs(title = "Mean Ride Time By User Type (Aug 2022 - July 2023)", x = "User Type",
y = "Mean Ride Time (minutes)") + geom_text(aes(x = member_casual, y = mean, label = mean),
vjust = -0.25, size = 5) + theme(text = element_text(size = 19))
# grouping the ride time by time variables: month, weekday, hour_started
weekday_member_casual_summary3 <- CyclisticClean %>%
group_by(month, member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),1), median = round(median(ride_time_minutes),1), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 8)
ggplot(data = weekday_member_casual_summary3, aes(x = month , y = mean, fill = member_casual), show.legend = FALSE) +
geom_col(position = "dodge", width = 0.7) +
labs(title = "Mean Ride Time by Month (Aug 2022 - July 2023)", x = "Month",y = "Mean Ride Time (minutes)") +
scale_fill_discrete(name="User Type") + theme_gray(base_size = 19) + geom_text(aes(label = mean),
size = 5, vjust = -0.25, position = position_dodge(width = 0.7))
weekday_member_casual_summary2 <- CyclisticClean %>%
group_by(weekday, member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),1), median = round(median(ride_time_minutes),1), .groups = "drop")
ggplot(data = weekday_member_casual_summary2, aes(x = weekday , y = mean, fill = member_casual),
show.legend = FALSE) + geom_col(position = "dodge", width = 0.8) +
labs(title = "Mean Ride Time by Day of the Week (Aug 2022 - July 2023)",
x = "Day",y = "Mean Ride Time (minutes)") + scale_fill_discrete(name="User Type") +
theme_gray(base_size = 19) + geom_text(aes(label = mean), size = 5, vjust = -0.25,
position = position_dodge(width = 0.8))
ride_time_summary_week <- CyclisticClean %>%
filter(weekday == "Monday" | weekday == "Tuesday" | weekday == "Wednesday" |
weekday == "Thursday" | weekday == "Friday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),1), median = round(median(ride_time_minutes),1), .groups = "drop")
ride_time_summary_weekend <- CyclisticClean %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),1), median = round(median(ride_time_minutes),1), .groups = "drop")
ggplot(data = ride_time_summary_week, aes(x = hour_started , y = mean, group = member_casual,
color = member_casual)) + geom_line() + geom_point() +
labs(title = "Mean Ride Time by Time of Day (Weekdays, Aug 2022 - July 2023)",
x = "Hour", y = "Mean Ride Time (minutes)") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "User Type")) +
geom_text(aes(x = hour_started, y = mean, label = mean), vjust = -1, size = 4)
ggplot(data = ride_time_summary_weekend, aes(x = hour_started , y = mean, group = member_casual,
color = member_casual)) + geom_line() + geom_point() +
labs(title = "Mean Ride Time by Time of Day (Weekends, Aug 2022 - July 2023)", x = "Hour",
y = "Mean Ride Time (minutes)") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "User Type")) +
geom_text(aes(x = hour_started, y = mean, label = mean), vjust = -1, size = 4)
# dive into the Cyclistics bike types and summarize how frequently they're being used by Cyclistic riders
CyclisticClean$rideable_type <- ordered(CyclisticClean$rideable_type, levels =
c("electric_bike", "classic_bike"))
rideable_type_summary <- CyclisticClean %>%
group_by(member_casual, rideable_type) %>%
summarise(count = n(), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 8)
ggplot(data = rideable_type_summary, aes(x = member_casual, y = count, fill = rideable_type)) +
geom_col(position = "stack") + labs(title = "Ride Count by Bike Type (Aug 2022 - Jul 2023)",
x = "User Type", y = "# of Rides") + scale_fill_discrete(name="Bike Type") +
theme_gray(base_size = 19) + coord_flip() + geom_text(aes(label = count), size = 5,
position = position_stack(vjust = 0.5))
# perform some multivariate analysis on rideable_type to get a clearer picture on how each User group uses each bike
rideable_type_summary3 <- CyclisticClean %>%
filter(rideable_type == "electric_bike" | rideable_type == "classic_bike") %>%
group_by(month, rideable_type, member_casual) %>%
summarise(count = n(), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 16)
ggplot(data = rideable_type_summary3, aes(x = month, y = count, fill = rideable_type)) +
geom_col(position = "stack") +
labs(title = "Ride Count by Bike Type (Months, Aug 2022 - Jul 2023)", x = "Bike Type", y = "# of Rides") +
scale_fill_discrete(name = "Bike Type") + facet_wrap(~member_casual, nrow = 2) +
theme_gray(base_size = 19) + geom_text(aes(label = count), size = 4.5, position = position_stack(vjust = 0.9))
rideable_type_summary2 <- CyclisticClean %>%
filter(rideable_type == "electric_bike" | rideable_type == "classic_bike") %>%
group_by(weekday, rideable_type, member_casual) %>%
summarise(count = n(), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 12)
ggplot(data = rideable_type_summary2, aes(x = member_casual, y = count, fill = rideable_type)) +
geom_col(position = "stack") + labs(title = "Ride Count by Bike Type (Weeks, Aug 2022 - Jul 2023)",
x = "User Type", y = "# of Rides") + scale_fill_discrete(name="Bike Type") +
facet_wrap(~weekday) + theme(axis.text.x = element_text(angle = 25)) + coord_flip() + theme_gray(base_size = 19) +
geom_text(aes(label = count), size = 5, position = position_stack(vjust = 0.5))
rideable_type_summary_weekday <- CyclisticClean %>%
filter(weekday == "Monday" | weekday == "Tuesday"| weekday == "Wednesday" | weekday == "Thursday" |
weekday == "Friday") %>%
group_by(hour_started, rideable_type, member_casual) %>%
summarise(count = n(), .groups = "drop")
rideable_type_summary_weekend <- CyclisticClean %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(hour_started, rideable_type, member_casual) %>%
summarise(count = n(), .groups = "drop")
ggplot(data = rideable_type_summary_weekday, aes(x = hour_started, y = count, group = rideable_type,
color = rideable_type)) + geom_line() + geom_point() +
labs(title = "Daily Ride Count by Bike Type (Weekdays, Aug 2022 - Jul 2023)", x = "Time of Day",
y = "# of Rides") + scale_fill_discrete(name="Bike Type") + facet_wrap(~member_casual, nrow = 2) +
theme(axis.text.x = element_text(angle = 90), text = element_text(size = 19)) +
guides(color = guide_legend(title = "Bike Type"))
ggplot(data = rideable_type_summary_weekend, aes(x = hour_started, y = count, group = rideable_type,
color = rideable_type)) + geom_line() + geom_point() +
labs(title = "Daily Ride Count by Bike Type (Weekends, Aug 2022 - Jul 2023)", x = "Time of Day",
y = "# of Rides") + scale_fill_discrete(name="Bike Type") + facet_wrap(~member_casual, nrow = 2) +
theme(axis.text.x = element_text(angle = 90), text = element_text(size = 19)) +
guides(color = guide_legend(title = "Bike Type"))
# look at the mean ride time for each bike type and group the data by User type
rideable_type_summary5 <- CyclisticClean %>%
group_by(rideable_type, member_casual) %>%
summarise(mean = round(mean(ride_time_minutes),2), .groups = "drop")
options(repr.plot.width = 10, repr.plot.height = 8)
ggplot(data = rideable_type_summary5, aes(x = rideable_type, y = mean, fill = rideable_type)) +
geom_col() + labs(title = "Mean Ride Time by Bike Type (Aug 2022 - Jul 2023)", x = "Bike Type",
y = "Ride Time (minutes)") + facet_wrap(~member_casual) + geom_text(aes(x = rideable_type,
y = mean, label = mean), vjust = -0.25) + theme(legend.position = "none", text = element_text(size = 19))
# analyze classic bike trip duration in more detail by incorporating some time variables
ridetime_summary_biketype1 <- CyclisticClean %>%
filter(member_casual == "casual") %>%
group_by(rideable_type, month) %>%
summarise(mean = round(mean(ride_time_minutes),1), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 8)
ggplot(data = ridetime_summary_biketype1, aes(x = month , y = mean, fill = rideable_type),
show.legend = FALSE) + geom_col(position = "dodge", width = 0.7) +
labs(title = "Mean Ride Time by Bike Type for Casual Users (Aug 2022 - July 2023)", x = "Month",
y = "Mean Ride Time (minutes)") + scale_fill_discrete(name="Bike Type") + theme_gray(base_size = 19) +
geom_text(aes(label = mean), size = 5, vjust = -0.25, position = position_dodge(width = 0.7))
ridetime_summary_biketype2 <- CyclisticClean %>%
filter(member_casual == "casual") %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(rideable_type, hour_started) %>%
summarise(mean = mean(ride_time_minutes), .groups = "drop")
ridetime_summary_biketype3 <- CyclisticClean %>%
filter(member_casual == "casual") %>%
filter(!weekday == "Saturday" | !weekday == "Sunday") %>%
group_by(rideable_type, hour_started) %>%
summarise(mean = mean(ride_time_minutes), .groups = "drop")
ggplot(data = ridetime_summary_biketype2, aes(x = hour_started, y = mean, group = rideable_type,
color = rideable_type)) + geom_line() + geom_point() +
labs(title = "Mean Ride Time by Bike Type for Casual Users (Weekends, Aug 2022 - July 2023)", x = "Hour",
y = "Ride Time (minutes)") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "Bike Type"))
ggplot(data = ridetime_summary_biketype3, aes(x = hour_started, y = mean, group = rideable_type,
color = rideable_type)) + geom_line() + geom_point() +
labs(title = "Mean Ride Time by Bike Type for Casual Users (Weekends, Aug 2022 - July 2023)", x = "Hour",
y = "Ride Time (minutes)") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "Bike Type"))
# analyze the distance between users starting and ending positions
# Rides where distance is 0 km indicates a trip where the user returned to their starting position (round trip)
# subset data to where this occurs and visualize to explore further
distance_summary <- CyclisticClean %>%
drop_na(distance_between_stations_km)
distance0 <- distance_summary %>%
filter(distance_between_stations_km == 0) %>%
filter(rideable_type == "classic_bike")
dist0_summary <- distance0 %>%
group_by(member_casual) %>%
summarise(count = n(), percentage = round(length(ride_id)/nrow(distance0)*100,2), .groups = "drop")
options(repr.plot.width = 10, repr.plot.height = 8)
ggplot(dist0_summary, aes(x= " ", y = count, fill = member_casual)) +
geom_col() + coord_polar(theta = "y") + theme_void(base_size = 19) +
geom_text(aes(label = paste(count, " --- ", percentage,"%")),
position = position_stack(vjust = 0.5), size = 6) +
labs(title ="Count of Round Trips for Classic Bikes by User Type",
subtitle = "(Aug 2022 - July 2023)") + scale_fill_discrete(name = "User Type")
distance0casual <- distance0 %>%
filter(member_casual == "casual")
summary(distance0$ride_time_minutes)
summary(distance0casual$ride_time_minutes)
# look at round trip occurences in relation to the time variables
distance0_2 <- distance0 %>%
group_by(month, member_casual) %>%
summarise(mean = mean(ride_time_minutes), count = n(),
percentage = round(length(ride_id)/nrow(distance0)*100,2), .groups = "drop")
options(repr.plot.width = 16, repr.plot.height = 8)
ggplot(data = distance0_2, aes(x = month, y = count, group = member_casual, color = member_casual)) +
geom_line() + geom_point() +
labs(title = "Count of Round Trips for Classic Bikes by Month (Aug 2022 - July 2023)", x = "Month",
y = "# of Rides") + theme(axis.text.x = element_text(angle = 25), text = element_text(size = 19)) +
guides(color = guide_legend(title = "User Type")) + geom_text(aes(x = month, y = count, label = count),
size = 4.5, vjust = -0.35)
distance0_1 <- distance0 %>%
group_by(weekday, member_casual) %>%
summarise(mean = mean(ride_time_minutes), count = n(), percentage = round(length(ride_id)/nrow(distance0)*100,2),
.groups = "drop")
ggplot(data = distance0_1, aes(x = weekday, y = count, fill = member_casual)) +
geom_col(position = "dodge", width = 0.8) + scale_fill_discrete(name="User Type") +
labs(title = "Count of Round Trips for Classic Bikes by Weekday (Aug 2022 - July 2023)", x = "Day of Week",
y = "# of Rides") + theme_gray(base_size = 19) + geom_text(aes(x = weekday, y = count, label = count),
size = 5, position = position_dodge(width = 0.8), vjust = -0.25)
distance0_3 <- distance0 %>%
filter(weekday == "Monday" | weekday == "Tuesday" | weekday == "Wednesday" | weekday == "Thursday" |
weekday == "Friday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = mean(ride_time_minutes), count = n(), .groups = "drop")
distance0_4 <- distance0 %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = mean(ride_time_minutes), count = n(), .groups = "drop")
ggplot(data = distance0_3, aes(x = hour_started, y = count, group = member_casual, color = member_casual)) +
geom_line() + geom_point() +
labs(title = "Count of Round Trips for Classic Bikes by Time of Day (Weekdays, Aug 2022 - July 2023)",
x = "Time of Day", y = "# of Rides") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "User Type"))
ggplot(data = distance0_4, aes(x = hour_started, y = count, group = member_casual, color = member_casual)) +
geom_line() + geom_point() +
labs(title = "Count of Round Trip Rides for Classic Bikes by Time of Day (Weekends, Aug 2022 - July 2023)",
x = "Time of Day", y = "# of Rides") + theme(axis.text.x = element_text(angle = 90),
text = element_text(size = 19)) + guides(color = guide_legend(title = "User Type"))
# compare summary stats for distance_between_stations_km for each group
distance_summary_casual <- distance_summary %>%
filter(member_casual =="casual")
distance_summary_member <- distance_summary %>%
filter(member_casual =="member")
print(paste("distance_between_stations_km - CASUAL:"))
summary(distance_summary_casual$distance_between_stations_km)
print(paste("distance_between_stations_km - MEMBER:"))
summary(distance_summary_member$distance_between_stations_km)
# group means by some additional variables (bike type, time components) to see if we can discover any differences
distance_summary_biketype <- distance_summary %>%
group_by(rideable_type, member_casual) %>%
summarise(mean = round(mean(distance_between_stations_km),2), .groups = "drop")
ggplot(data = distance_summary_biketype, aes(x= rideable_type, y = mean, fill = rideable_type)) +
geom_col() + facet_wrap(~member_casual) +
geom_text(aes(x = rideable_type, y = mean, label = mean), vjust = -0.25, size = 5) +
theme(legend.position = "none", text = element_text(size = 19)) +
labs(title = "Mean Distance Between Start and End Point (Aug 2022 - July 2023)", x = "Bike Type",
y = "Distance Between Start/End Point (km)")
distance_summary_weekday <- distance_summary %>%
filter(weekday == "Monday" | weekday == "Tuesday"| weekday == "Wednesday" |
weekday == "Thursday" |weekday == "Friday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = round(mean(distance_between_stations_km),2), .groups = "drop")
distance_summary_weekends <- distance_summary %>%
filter(weekday == "Saturday" | weekday == "Sunday") %>%
group_by(hour_started, member_casual) %>%
summarise(mean = round(mean(distance_between_stations_km),2), .groups = "drop")
ggplot(data = distance_summary_weekday, aes(x= hour_started, y = mean, group = member_casual,
color = member_casual)) + geom_line() + geom_point() +
labs(title = "Mean Distance Between Start and End Point (Weekdays, Aug 2022 - July 2023)",
x = "Time of Day", y = "Distance Between Start/End Point (km)") +
theme(axis.text.x = element_text(angle = 90), text = element_text(size = 19)) + ylim(1.5,3) +
guides(color = guide_legend(title = "User Type"))
ggplot(data = distance_summary_weekends, aes(x= hour_started, y = mean, group = member_casual,
color = member_casual)) + geom_line() + geom_point() + theme_gray(base_size = 19) +
labs(title = "Mean Distance Between Start and End Point (Weekends, Aug 2022 - July 2023)",
x = "Time of Day", y = "Distance Between Start/End Point (km)") +
theme(axis.text.x = element_text(angle = 90), text = element_text(size = 19)) + ylim(1.5,3) +
guides(color = guide_legend(title = "User Type"))
# visualize where in the City of Chicago our riders are using our bikes.
# Group the data by starting and ending coordinates and filter the highest counts to create a summary of the
# most popular routes.
# create a summary for the most popular coordinates where round trips take place.
route_summary_dist0 <- distance0 %>%
group_by(start_lng, start_lat, end_lng, end_lat, member_casual) %>%
summarise(total = n(),.groups="drop") %>%
filter(total>100) %>%
arrange(desc(total))
route_summary <- distance_summary %>%
filter(!distance_between_stations_km == 0) %>%
group_by(start_lng, start_lat, end_lng, end_lat, member_casual) %>%
summarise(total = n(),.groups="drop") %>%
filter(total>200) %>%
arrange(desc(total))
# code for updating ggmap to access tiles from stadiamaps:
#remove.packages("ggmap")
#install.packages("devtools")
#devtools::install_github("stadiamaps/ggmap")
#register_stadiamaps("your-api-key-here")
#library(ggmap)
# create the following:
# Geospatial viz highlighting the most popular stations used for round trips for each User type
# Geospatial viz highlighting the most popular routes, excluding round trips for each User type
# source for defining coordinates of bbox arguement : https://www.openstreetmap.org/export#map=10/41.8338/-87.7327
chicago <- c(left = -87.77, bottom = 41.75, right = -87.55, top = 42.10)
chimap <- get_stadiamap(chicago, zoom = 12, maptype = "alidade_smooth_dark")
ggmap(chimap) +
geom_point(route_summary_dist0, mapping = aes(x = start_lng, y = start_lat, size = total,
color = member_casual, alpha = 0)) +
labs(title = "Popular Stations for Round Trips (Classic Bikes, Ride Count>100)", x=NULL, y=NULL) +
theme(legend.position="none") + facet_wrap(~member_casual) + scale_size(range = c(1,8)) + coord_fixed(0.7)
ggmap(chimap) +
geom_segment(route_summary, mapping = aes(x = start_lng, y = start_lat, xend = end_lng, yend = end_lat, color = member_casual)) +
labs(title = "Popular Routes, Excluding Round Trips (Ride Count>200)", x=NULL, y=NULL) + coord_fixed(0.7) +
theme(legend.position="none") + facet_wrap(~member_casual)