title:“GDA-CapstoneProject-Cyclistic”
author:“WhitanyaAlexander”
date:“2024-01-27”
output:html_document

Cyclistic Bike Share Capstone

## Ask
### Introduction

In 2016, Cyclistic (a fictional company) launched a successful bike-share offerings. Previously, Cyclistic’s marketing strategy relied on building general awareness and appealing to broad consumer segments, resulting in a mix of casual and member ridership. A financial analysis revealed that a greater portion of profit is made on member riders than causal riders.

Director of marketing Lily Moreno believes the future growth of the company relies on converting casual riders to member riders. Three questions will guide the future marketing campaign:

1. How do annual members and casual riders use Cyclistic bikes differently? 2. Why would casual riders buy Cyclistic annual memberships? 3. How can Cyclistic use digital media to influence casual riders to become members?

I, the junior data analyst on Moreno’s staff, have been tasked with addressing the first question so that a campaign can be designed to entice casual riders to become members.

Problem: Members have a higher lifetime value than casual riders, therefore how do we convert casual riders into members?

Business Task: To discover how casual and members use Cyclistic bikes differently to create a marketing campaign targeting the casual segment.

Stakeholders: Director of Marketing Lily Moreno

Mission & Vision: To increase Cyclistic future profitability by incentivising and converting casual members, who are familiar with Cyclistic’s products and benefits, into members, therefore increasing the lifetime value of casual riders.

## Prepare

As instructed, I have downloaded twelve previous months (January 2023 to December 2023) provided to Cyclistic by Motivate International under license (see license agreement below).

https://www.divvybikes.com/data-license-agreement

The data files are zip files separated by month. The files were renamed because they each began with a numeral. Then I set the working directory.

I received error messages when loading files so I set the R Cran directory.

local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cran.rstudio.com"
  repos <- unname(unlist(r))
  options(repos = repos) 
})

Next I installed the packages i will be using for my analysis…

## installing packages
install.packages("Rtools")##to remedy errors in R Studio Desktop
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: package 'Rtools' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
install.packages("tidyverse")##to manipulate data
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\admin\AppData\Local\Temp\RtmpoR59AD\downloaded_packages
install.packages("ggmap")##to plot start and end bike stations
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'ggmap' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\admin\AppData\Local\Temp\RtmpoR59AD\downloaded_packages

Then each of the packages was loaded…

## loading packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(ggplot2)
library(dplyr)
library(ggmap)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
##   Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
##   OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(readr)
library(knitr)
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Loading required package: RSQLite
knitr::knit_engines$set(rsetup = function(options) {
  knitr::opts_chunk$set(echo = TRUE, results = 'markup', tidy = TRUE) 
})

To begin preparing the trip data,each of the files needed to bet loaded and reviewed.I needed a single data frame of all files.I used r for this purpose.

First,i read all the CSV files from 01/2023 through 12/2023 into its own data frame.I manually updated each of the file names from “202301-dvvy_tripdata.csv” to “td012023.”

## reading each csv into a data frame
td012023 <- read.csv("td012023.csv", header = TRUE)
td022023 <- read.csv("td022023.csv", header = TRUE)
td032023 <- read.csv("td032023.csv", header = TRUE)
td042023 <- read.csv("td042023.csv", header = TRUE)
td052023 <- read.csv("td052023.csv", header = TRUE)
td062023 <- read.csv("td062023.csv", header = TRUE)
td072023 <- read.csv("td072023.csv", header = TRUE)
td082023 <- read.csv("td082023.csv", header = TRUE)
td092023 <- read.csv("td092023.csv", header = TRUE)
td102023 <- read.csv("td102023.csv", header = TRUE)
td112023 <- read.csv("td112023.csv", header = TRUE)
td122023 <- read.csv("td122023.csv", header = TRUE)

Second, I check the column names of each data frame

  colnames(td012023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td022023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td032023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td042023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td052023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td062023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td072023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td082023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td092023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td102023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td112023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"
  colnames(td122023)
##  [1] "ride_id"            "rideable_type"      "started_at"        
##  [4] "ended_at"           "start_station_name" "start_station_id"  
##  [7] "end_station_name"   "end_station_id"     "start_lat"         
## [10] "start_lng"          "end_lat"            "end_lng"           
## [13] "member_casual"

After confirming all of the column names matched, I appended the files using r.

## append all data trip data into a single file
all_tripdata <- rbind(
  td012023,
  td022023,
  td032023,
  td042023,
  td052023,
  td062023,
  td072023,
  td082023,
  td092023,
  td102023,
  td112023,
  td122023,
  collapse="\n"
  )

str(all_tripdata)
## 'data.frame':    5719878 obs. of  13 variables:
##  $ ride_id           : chr  "F96D5A74A3E41399" "13CB7EB698CEDB88" "BD88A2E670661CE5" "C90792D034FED968" ...
##  $ rideable_type     : chr  "electric_bike" "classic_bike" "electric_bike" "classic_bike" ...
##  $ started_at        : chr  "2023-01-21 20:05:42" "2023-01-10 15:37:36" "2023-01-02 07:51:57" "2023-01-22 10:52:58" ...
##  $ ended_at          : chr  "2023-01-21 20:16:33" "2023-01-10 15:46:05" "2023-01-02 08:05:11" "2023-01-22 11:01:44" ...
##  $ start_station_name: chr  "Lincoln Ave & Fullerton Ave" "Kimbark Ave & 53rd St" "Western Ave & Lunt Ave" "Kimbark Ave & 53rd St" ...
##  $ start_station_id  : chr  "TA1309000058" "TA1309000037" "RP-005" "TA1309000037" ...
##  $ end_station_name  : chr  "Hampden Ct & Diversey Ave" "Greenwood Ave & 47th St" "Valli Produce - Evanston Plaza" "Greenwood Ave & 47th St" ...
##  $ end_station_id    : chr  "202480.0" "TA1308000002" "599" "TA1308000002" ...
##  $ start_lat         : chr  "41.924073935" "41.799568" "42.008571" "41.799568" ...
##  $ start_lng         : chr  "-87.646278381" "-87.594747" "-87.6904828333333" "-87.594747" ...
##  $ end_lat           : chr  "41.93" "41.809835" "42.039742" "41.809835" ...
##  $ end_lng           : chr  "-87.64" "-87.599383" "-87.699413" "-87.599383" ...
##  $ member_casual     : chr  "member" "member" "casual" "member" ...

This created a data frame with 13 columns and 5,719,878 rows. I decided to keep a copy of the copy of the complete un-processed data frame as a CSV called all_tripdata.csv

## creating a copy of complete un-processed data frame
write.csv(all_tripdata, file = "C:/users/admin/Desktop/Cyclistic - GDA/TripData/all_tripdata.csv", row.names = FALSE)

## Process

The structure of the data frame revealed all columns are character data type. To perform calculations on the started_at and ended_at columns, which gives the start and end date-times of each ride combined, I converted them to date/time format then split each column into a start date and start time columns as well as an end date and end time columns.

# Convert 'started_at' and 'ended_at' to POSIXlt objects
all_tripdata$started_at <- as.POSIXlt(all_tripdata$started_at, format = "%Y-%m-%d %H:%M:%S")
all_tripdata$ended_at <- as.POSIXlt(all_tripdata$ended_at, format = "%Y-%m-%d %H:%M:%S")
# Create separate date and time columns
all_tripdata <- all_tripdata %>%
  mutate(
    start_date = format(started_at, "%m/%d/%Y"),
    start_time = format(started_at, "%H:%M"),
    end_date = format(ended_at, "%m/%d/%Y"),
    end_time = format(ended_at, "%H:%M")
  )

head(all_tripdata)
##            ride_id rideable_type          started_at            ended_at
## 1 F96D5A74A3E41399 electric_bike 2023-01-21 20:05:42 2023-01-21 20:16:33
## 2 13CB7EB698CEDB88  classic_bike 2023-01-10 15:37:36 2023-01-10 15:46:05
## 3 BD88A2E670661CE5 electric_bike 2023-01-02 07:51:57 2023-01-02 08:05:11
## 4 C90792D034FED968  classic_bike 2023-01-22 10:52:58 2023-01-22 11:01:44
## 5 3397017529188E8A  classic_bike 2023-01-12 13:58:01 2023-01-12 14:13:20
## 6 58E68156DAE3E311 electric_bike 2023-01-31 07:18:03 2023-01-31 07:21:16
##              start_station_name start_station_id               end_station_name
## 1   Lincoln Ave & Fullerton Ave     TA1309000058      Hampden Ct & Diversey Ave
## 2         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 3        Western Ave & Lunt Ave           RP-005 Valli Produce - Evanston Plaza
## 4         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 5         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 6 Lakeview Ave & Fullerton Pkwy     TA1309000019      Hampden Ct & Diversey Ave
##   end_station_id    start_lat         start_lng   end_lat    end_lng
## 1       202480.0 41.924073935     -87.646278381     41.93     -87.64
## 2   TA1308000002    41.799568        -87.594747 41.809835 -87.599383
## 3            599    42.008571 -87.6904828333333 42.039742 -87.699413
## 4   TA1308000002    41.799568        -87.594747 41.809835 -87.599383
## 5   TA1308000002    41.799568        -87.594747 41.809835 -87.599383
## 6       202480.0 41.926068902     -87.638858199     41.93     -87.64
##   member_casual start_date start_time   end_date end_time
## 1        member 01/21/2023      20:05 01/21/2023    20:16
## 2        member 01/10/2023      15:37 01/10/2023    15:46
## 3        casual 01/02/2023      07:51 01/02/2023    08:05
## 4        member 01/22/2023      10:52 01/22/2023    11:01
## 5        member 01/12/2023      13:58 01/12/2023    14:13
## 6        member 01/31/2023      07:18 01/31/2023    07:21

Next, to clean up the appearance, the start_lat, start_lng, end_lat, and end_lng were converted to doubles, rounded to 2 decimal places.

## converting latitude and longitude to doubles
all_tripdata2 <- all_tripdata %>%
  mutate(start_lat = round(as.numeric(start_lat), 2),
          start_lng = round(as.numeric(start_lng), 2),
          end_lat = round(as.numeric(end_lat), 2),
          end_lng = round(as.numeric(end_lng), 2))
head(all_tripdata2)
##            ride_id rideable_type          started_at            ended_at
## 1 F96D5A74A3E41399 electric_bike 2023-01-21 20:05:42 2023-01-21 20:16:33
## 2 13CB7EB698CEDB88  classic_bike 2023-01-10 15:37:36 2023-01-10 15:46:05
## 3 BD88A2E670661CE5 electric_bike 2023-01-02 07:51:57 2023-01-02 08:05:11
## 4 C90792D034FED968  classic_bike 2023-01-22 10:52:58 2023-01-22 11:01:44
## 5 3397017529188E8A  classic_bike 2023-01-12 13:58:01 2023-01-12 14:13:20
## 6 58E68156DAE3E311 electric_bike 2023-01-31 07:18:03 2023-01-31 07:21:16
##              start_station_name start_station_id               end_station_name
## 1   Lincoln Ave & Fullerton Ave     TA1309000058      Hampden Ct & Diversey Ave
## 2         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 3        Western Ave & Lunt Ave           RP-005 Valli Produce - Evanston Plaza
## 4         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 5         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 6 Lakeview Ave & Fullerton Pkwy     TA1309000019      Hampden Ct & Diversey Ave
##   end_station_id start_lat start_lng end_lat end_lng member_casual start_date
## 1       202480.0     41.92    -87.65   41.93  -87.64        member 01/21/2023
## 2   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/10/2023
## 3            599     42.01    -87.69   42.04  -87.70        casual 01/02/2023
## 4   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/22/2023
## 5   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/12/2023
## 6       202480.0     41.93    -87.64   41.93  -87.64        member 01/31/2023
##   start_time   end_date end_time
## 1      20:05 01/21/2023    20:16
## 2      15:37 01/10/2023    15:46
## 3      07:51 01/02/2023    08:05
## 4      10:52 01/22/2023    11:01
## 5      13:58 01/12/2023    14:13
## 6      07:18 01/31/2023    07:21

A ride_time column that calculates the length of time in minutes of each ride length was created using the original started_at and ended_at columns. I also created columns for month so data can be filtered chronologically.

# calculating ride_time in minutes 
all_tripdata2$ride_time_mins <- round(as.numeric(difftime(all_tripdata2$ended_at, all_tripdata$started_at, units = "mins")),0)

## calculating started_at day, date, week, month and season of a ride
all_tripdata2$s_date <- as.Date(all_tripdata2$started_at)
all_tripdata2$s_month <- format(as.Date(all_tripdata2$s_date), "%m")


## calculating ended_at day, date, week, month and season of a ride
all_tripdata2$e_date <- as.Date(all_tripdata2$ended_at)
all_tripdata2$e_month <- format(as.Date(all_tripdata2$e_date), "%m")

Added seasons to rides by month.

## added seasons to the months for seasonal comparison
all_tripdata2 <- all_tripdata2 %>% mutate(seasons = case_when(
    s_month == "01" ~ "Winter",
    s_month == "02" ~ "Winter",
    s_month == "03" ~ "Spring",
    s_month == "04" ~ "Spring",
    s_month == "05" ~ "Spring",
    s_month == "06" ~ "Summer",
    s_month == "07" ~ "Summer",
    s_month == "08" ~ "Summer",
    s_month == "09" ~ "Fall",
    s_month == "10" ~ "Fall",
    s_month == "11" ~ "Fall",
    s_month == "12" ~ "Winter"))

head(all_tripdata2)
##            ride_id rideable_type          started_at            ended_at
## 1 F96D5A74A3E41399 electric_bike 2023-01-21 20:05:42 2023-01-21 20:16:33
## 2 13CB7EB698CEDB88  classic_bike 2023-01-10 15:37:36 2023-01-10 15:46:05
## 3 BD88A2E670661CE5 electric_bike 2023-01-02 07:51:57 2023-01-02 08:05:11
## 4 C90792D034FED968  classic_bike 2023-01-22 10:52:58 2023-01-22 11:01:44
## 5 3397017529188E8A  classic_bike 2023-01-12 13:58:01 2023-01-12 14:13:20
## 6 58E68156DAE3E311 electric_bike 2023-01-31 07:18:03 2023-01-31 07:21:16
##              start_station_name start_station_id               end_station_name
## 1   Lincoln Ave & Fullerton Ave     TA1309000058      Hampden Ct & Diversey Ave
## 2         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 3        Western Ave & Lunt Ave           RP-005 Valli Produce - Evanston Plaza
## 4         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 5         Kimbark Ave & 53rd St     TA1309000037        Greenwood Ave & 47th St
## 6 Lakeview Ave & Fullerton Pkwy     TA1309000019      Hampden Ct & Diversey Ave
##   end_station_id start_lat start_lng end_lat end_lng member_casual start_date
## 1       202480.0     41.92    -87.65   41.93  -87.64        member 01/21/2023
## 2   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/10/2023
## 3            599     42.01    -87.69   42.04  -87.70        casual 01/02/2023
## 4   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/22/2023
## 5   TA1308000002     41.80    -87.59   41.81  -87.60        member 01/12/2023
## 6       202480.0     41.93    -87.64   41.93  -87.64        member 01/31/2023
##   start_time   end_date end_time ride_time_mins     s_date s_month     e_date
## 1      20:05 01/21/2023    20:16             11 2023-01-21      01 2023-01-21
## 2      15:37 01/10/2023    15:46              8 2023-01-10      01 2023-01-10
## 3      07:51 01/02/2023    08:05             13 2023-01-02      01 2023-01-02
## 4      10:52 01/22/2023    11:01              9 2023-01-22      01 2023-01-22
## 5      13:58 01/12/2023    14:13             15 2023-01-12      01 2023-01-12
## 6      07:18 01/31/2023    07:21              3 2023-01-31      01 2023-01-31
##   e_month seasons
## 1      01  Winter
## 2      01  Winter
## 3      01  Winter
## 4      01  Winter
## 5      01  Winter
## 6      01  Winter

Because the started_at and ended_at columns were no longer needed for analysis, they were removed.

## removing columns started_at and ended_at
all_tripdata3 <- select(all_tripdata2, -"started_at", -"ended_at")

head(all_tripdata3)
##            ride_id rideable_type            start_station_name start_station_id
## 1 F96D5A74A3E41399 electric_bike   Lincoln Ave & Fullerton Ave     TA1309000058
## 2 13CB7EB698CEDB88  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 3 BD88A2E670661CE5 electric_bike        Western Ave & Lunt Ave           RP-005
## 4 C90792D034FED968  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 5 3397017529188E8A  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 6 58E68156DAE3E311 electric_bike Lakeview Ave & Fullerton Pkwy     TA1309000019
##                 end_station_name end_station_id start_lat start_lng end_lat
## 1      Hampden Ct & Diversey Ave       202480.0     41.92    -87.65   41.93
## 2        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 3 Valli Produce - Evanston Plaza            599     42.01    -87.69   42.04
## 4        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 5        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 6      Hampden Ct & Diversey Ave       202480.0     41.93    -87.64   41.93
##   end_lng member_casual start_date start_time   end_date end_time
## 1  -87.64        member 01/21/2023      20:05 01/21/2023    20:16
## 2  -87.60        member 01/10/2023      15:37 01/10/2023    15:46
## 3  -87.70        casual 01/02/2023      07:51 01/02/2023    08:05
## 4  -87.60        member 01/22/2023      10:52 01/22/2023    11:01
## 5  -87.60        member 01/12/2023      13:58 01/12/2023    14:13
## 6  -87.64        member 01/31/2023      07:18 01/31/2023    07:21
##   ride_time_mins     s_date s_month     e_date e_month seasons
## 1             11 2023-01-21      01 2023-01-21      01  Winter
## 2              8 2023-01-10      01 2023-01-10      01  Winter
## 3             13 2023-01-02      01 2023-01-02      01  Winter
## 4              9 2023-01-22      01 2023-01-22      01  Winter
## 5             15 2023-01-12      01 2023-01-12      01  Winter
## 6              3 2023-01-31      01 2023-01-31      01  Winter

The label of the member_casual which tells what type of rider made the ride data, was changed to member_status.

colnames(all_tripdata3)[colnames(all_tripdata3) == "member_casual"] <- "member_status"

head(all_tripdata3)
##            ride_id rideable_type            start_station_name start_station_id
## 1 F96D5A74A3E41399 electric_bike   Lincoln Ave & Fullerton Ave     TA1309000058
## 2 13CB7EB698CEDB88  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 3 BD88A2E670661CE5 electric_bike        Western Ave & Lunt Ave           RP-005
## 4 C90792D034FED968  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 5 3397017529188E8A  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 6 58E68156DAE3E311 electric_bike Lakeview Ave & Fullerton Pkwy     TA1309000019
##                 end_station_name end_station_id start_lat start_lng end_lat
## 1      Hampden Ct & Diversey Ave       202480.0     41.92    -87.65   41.93
## 2        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 3 Valli Produce - Evanston Plaza            599     42.01    -87.69   42.04
## 4        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 5        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 6      Hampden Ct & Diversey Ave       202480.0     41.93    -87.64   41.93
##   end_lng member_status start_date start_time   end_date end_time
## 1  -87.64        member 01/21/2023      20:05 01/21/2023    20:16
## 2  -87.60        member 01/10/2023      15:37 01/10/2023    15:46
## 3  -87.70        casual 01/02/2023      07:51 01/02/2023    08:05
## 4  -87.60        member 01/22/2023      10:52 01/22/2023    11:01
## 5  -87.60        member 01/12/2023      13:58 01/12/2023    14:13
## 6  -87.64        member 01/31/2023      07:18 01/31/2023    07:21
##   ride_time_mins     s_date s_month     e_date e_month seasons
## 1             11 2023-01-21      01 2023-01-21      01  Winter
## 2              8 2023-01-10      01 2023-01-10      01  Winter
## 3             13 2023-01-02      01 2023-01-02      01  Winter
## 4              9 2023-01-22      01 2023-01-22      01  Winter
## 5             15 2023-01-12      01 2023-01-12      01  Winter
## 6              3 2023-01-31      01 2023-01-31      01  Winter

The variables name casual was changed to non-=member

## changing casual variable into non-member
all_tripdata3 <- all_tripdata3 %>%
mutate(member_status = ifelse(member_status == "casual", "non-member", "member"))

head(all_tripdata3)
##            ride_id rideable_type            start_station_name start_station_id
## 1 F96D5A74A3E41399 electric_bike   Lincoln Ave & Fullerton Ave     TA1309000058
## 2 13CB7EB698CEDB88  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 3 BD88A2E670661CE5 electric_bike        Western Ave & Lunt Ave           RP-005
## 4 C90792D034FED968  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 5 3397017529188E8A  classic_bike         Kimbark Ave & 53rd St     TA1309000037
## 6 58E68156DAE3E311 electric_bike Lakeview Ave & Fullerton Pkwy     TA1309000019
##                 end_station_name end_station_id start_lat start_lng end_lat
## 1      Hampden Ct & Diversey Ave       202480.0     41.92    -87.65   41.93
## 2        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 3 Valli Produce - Evanston Plaza            599     42.01    -87.69   42.04
## 4        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 5        Greenwood Ave & 47th St   TA1308000002     41.80    -87.59   41.81
## 6      Hampden Ct & Diversey Ave       202480.0     41.93    -87.64   41.93
##   end_lng member_status start_date start_time   end_date end_time
## 1  -87.64        member 01/21/2023      20:05 01/21/2023    20:16
## 2  -87.60        member 01/10/2023      15:37 01/10/2023    15:46
## 3  -87.70    non-member 01/02/2023      07:51 01/02/2023    08:05
## 4  -87.60        member 01/22/2023      10:52 01/22/2023    11:01
## 5  -87.60        member 01/12/2023      13:58 01/12/2023    14:13
## 6  -87.64        member 01/31/2023      07:18 01/31/2023    07:21
##   ride_time_mins     s_date s_month     e_date e_month seasons
## 1             11 2023-01-21      01 2023-01-21      01  Winter
## 2              8 2023-01-10      01 2023-01-10      01  Winter
## 3             13 2023-01-02      01 2023-01-02      01  Winter
## 4              9 2023-01-22      01 2023-01-22      01  Winter
## 5             15 2023-01-12      01 2023-01-12      01  Winter
## 6              3 2023-01-31      01 2023-01-31      01  Winter

Now the column names with revised to simplify the variables and columns titles

all_tripdata4 <- all_tripdata3 %>%
   rename(bike_type = "rideable_type",
          start_station = "start_station_name",
          start_id = "start_station_id",
          end_station = "end_station_name",
          end_id = "end_station_id")

all_tripdata4 <- all_tripdata4 %>%
  mutate(bike_type = recode(bike_type,
                       "classic_bike" = "classic",
                       "electric_bike" = "electric",
                       "docked_bike" = "docked"))
head(all_tripdata4)
##            ride_id bike_type                 start_station     start_id
## 1 F96D5A74A3E41399  electric   Lincoln Ave & Fullerton Ave TA1309000058
## 2 13CB7EB698CEDB88   classic         Kimbark Ave & 53rd St TA1309000037
## 3 BD88A2E670661CE5  electric        Western Ave & Lunt Ave       RP-005
## 4 C90792D034FED968   classic         Kimbark Ave & 53rd St TA1309000037
## 5 3397017529188E8A   classic         Kimbark Ave & 53rd St TA1309000037
## 6 58E68156DAE3E311  electric Lakeview Ave & Fullerton Pkwy TA1309000019
##                      end_station       end_id start_lat start_lng end_lat
## 1      Hampden Ct & Diversey Ave     202480.0     41.92    -87.65   41.93
## 2        Greenwood Ave & 47th St TA1308000002     41.80    -87.59   41.81
## 3 Valli Produce - Evanston Plaza          599     42.01    -87.69   42.04
## 4        Greenwood Ave & 47th St TA1308000002     41.80    -87.59   41.81
## 5        Greenwood Ave & 47th St TA1308000002     41.80    -87.59   41.81
## 6      Hampden Ct & Diversey Ave     202480.0     41.93    -87.64   41.93
##   end_lng member_status start_date start_time   end_date end_time
## 1  -87.64        member 01/21/2023      20:05 01/21/2023    20:16
## 2  -87.60        member 01/10/2023      15:37 01/10/2023    15:46
## 3  -87.70    non-member 01/02/2023      07:51 01/02/2023    08:05
## 4  -87.60        member 01/22/2023      10:52 01/22/2023    11:01
## 5  -87.60        member 01/12/2023      13:58 01/12/2023    14:13
## 6  -87.64        member 01/31/2023      07:18 01/31/2023    07:21
##   ride_time_mins     s_date s_month     e_date e_month seasons
## 1             11 2023-01-21      01 2023-01-21      01  Winter
## 2              8 2023-01-10      01 2023-01-10      01  Winter
## 3             13 2023-01-02      01 2023-01-02      01  Winter
## 4              9 2023-01-22      01 2023-01-22      01  Winter
## 5             15 2023-01-12      01 2023-01-12      01  Winter
## 6              3 2023-01-31      01 2023-01-31      01  Winter
nrow(all_tripdata4)
## [1] 5719878

Then missing values were removed. To determine how much of the data frame all_tripdata4 has missing values, the VIM package was installed to visualize the data.

## installing and loading VIM

install.packages("VIM")
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'VIM' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'VIM'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\admin\AppData\Local\R\win-library\4.3\00LOCK\VIM\libs\x64\VIM.dll to
## C:\Users\admin\AppData\Local\R\win-library\4.3\VIM\libs\x64\VIM.dll: Permission
## denied
## Warning: restored 'VIM'
## 
## The downloaded binary packages are in
##  C:\Users\admin\AppData\Local\Temp\RtmpoR59AD\downloaded_packages
library(VIM)
## Loading required package: colorspace
## Loading required package: grid
## VIM is ready to use.
## Suggestions and bug-reports can be submitted at: https://github.com/statistikat/VIM/issues
## 
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
## 
##     sleep
## visualizing missing values

all_tripdata4 <- all_tripdata4
mp <- aggr(all_tripdata4, col=c("lightblue","orange"),
           numbers=TRUE, sortVars=TRUE,
           labels=names(all_tripdata4), cex.axis=.7,
            gap = 3, ylab=c("Missing data","Patterns"))
## Warning in plot.aggr(res, ...): not enough horizontal space to display
## frequencies

## 
##  Variables sorted by number of missings: 
##        Variable        Count
##         end_lat 1.222229e-03
##         end_lng 1.222229e-03
##       start_lat 1.748289e-07
##       start_lng 1.748289e-07
##      start_date 1.748289e-07
##      start_time 1.748289e-07
##        end_date 1.748289e-07
##        end_time 1.748289e-07
##  ride_time_mins 1.748289e-07
##          s_date 1.748289e-07
##         s_month 1.748289e-07
##          e_date 1.748289e-07
##         e_month 1.748289e-07
##         seasons 1.748289e-07
##         ride_id 0.000000e+00
##       bike_type 0.000000e+00
##   start_station 0.000000e+00
##        start_id 0.000000e+00
##     end_station 0.000000e+00
##          end_id 0.000000e+00
##   member_status 0.000000e+00

Because the amount of missing values were not significant, and the data set has 5 million-plus rows, all incomplete rows were removed using the complete.cases function.

## filtering out rows with missing values
all_tripdata5 <- all_tripdata4[complete.cases(all_tripdata4), ]

nrow(all_tripdata5)
## [1] 5712887

This reduced the data frame to 5,712,887 or 0.02%. Next rows containing NA were removed. Also, the data frame was filtered for duplicate rows.

## removing NA from data
all_tripdata5 <- na.omit(all_tripdata5)

## removing duplicates
all_tripdata5 <- distinct(all_tripdata5)

nrow(all_tripdata5)
## [1] 5712887

Data frame remained 5,712,887 rows. As one final measure to ensure the integrity of the data, all rows were filtered for blanks and NAs.

## filtering rows for blanks
all_tripdata5 <- all_tripdata5 %>%
  filter(!is.na(end_lat) & end_lat != "",
        !is.na(end_lng) & end_lng != "",
        !is.na(end_id) & end_id != "",
        !is.na(end_station) & end_station != "",
        !is.na(start_lng) & start_lng != "",
        !is.na(start_lat) & start_lat != "",
        !is.na(start_id) & start_id != "",
        !is.na(start_station) & start_station != "")

nrow(all_tripdata5)
## [1] 4331707

Data frame remained 4,331,707 rows (observations). To be certain all blanks and NA were removed, the data set was visualized once more.

## re-visualizing missing values for data integrity

all_tripdata5 <- all_tripdata5
mp <- aggr(all_tripdata5, col=c("lightblue","orange"),
           numbers=TRUE, sortVars=TRUE,
           labels=names(all_tripdata5), cex.axis=.7,
            gap = 3, ylab=c("Missing data","Patterns"))

## 
##  Variables sorted by number of missings: 
##        Variable Count
##         ride_id     0
##       bike_type     0
##   start_station     0
##        start_id     0
##     end_station     0
##          end_id     0
##       start_lat     0
##       start_lng     0
##         end_lat     0
##         end_lng     0
##   member_status     0
##      start_date     0
##      start_time     0
##        end_date     0
##        end_time     0
##  ride_time_mins     0
##          s_date     0
##         s_month     0
##          e_date     0
##         e_month     0
##         seasons     0
## reviewing summary of data
nrow(all_tripdata5)
## [1] 4331707

I noted in the summary that there are negative numbers in the ride_time_mins columns so the negative numbers were filtered out.

## filtering ride_time_mins for negative values
all_tripdata6 <- all_tripdata5 %>%
  filter(ride_time_mins >= 0)

nrow(all_tripdata6)
## [1] 4331673

The data frame all_tripdata6 was reduced to 4,331,673 or 75.7% of the original 5,719,878 rows.

The clean data frame was converted into a CSV file “clean_td.csv” in case it is needed for further analysis.

## creating CSV file "clean_td.csv"
write.csv(all_tripdata6, file = "C:/users/admin/Desktop/Cyclistic - GDA/TripData/clean_td.csv", row.names = FALSE)

## Analysis and Visualization

First, the analysis started with exploring the data observe what the data tells us about how the riders behave. I started with the basics about the rides taken by each member_type:

+ Total rides in 2023
+ How many of those rides were non-members vs members?
+ How many of those rides were taken by each type of bike?
+ What was the total and average ride time for each member type?
+ Which bike each member type preferred?
install.packages("scales")
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'scales' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\admin\AppData\Local\Temp\RtmpoR59AD\downloaded_packages
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
## Total recorded rides
tripdata_2023 <- all_tripdata6

## count of members
mem_cnt = tripdata_2023 %>%
  count(member_status)

## rides by bike type
rides_by_bike = tripdata_2023 %>%
  group_by(bike_type) %>%
  count(bike_type)

## member count by bike type
mem_by_bike = tripdata_2023 %>%
  group_by(member_status) %>%
  count(bike_type)

print(mem_cnt)
##   member_status       n
## 1        member 2799938
## 2    non-member 1531735
print(rides_by_bike)
## # A tibble: 3 × 2
## # Groups:   bike_type [3]
##   bike_type       n
##   <chr>       <int>
## 1 classic   2690719
## 2 docked      76124
## 3 electric  1564830
print(mem_by_bike)
## # A tibble: 5 × 3
## # Groups:   member_status [2]
##   member_status bike_type       n
##   <chr>         <chr>       <int>
## 1 member        classic   1817711
## 2 member        electric   982227
## 3 non-member    classic    873008
## 4 non-member    docked      76124
## 5 non-member    electric   582603

This bar chart visualizes the total member count.

## visualizing total member numbers
ggplot(data = tripdata_2023) +
  geom_bar( aes(x = member_status, fill = member_status), stat = "count", show.legend = FALSE) +
   geom_text(
    aes(x = member_status, label = after_stat(count)),
    stat = "count",
    position = position_stack(vjust = 0.75),
    size = 5,
    color = "black"
  ) +
  scale_y_continuous(
    labels = scales::label_number(
      scale = 1, 
      suffix = " rides",
      big.mark = ","
    )
  ) +
  scale_fill_manual(values = c( "lightblue", "pink")) 

  labs(title = "Total Member Count")
## $title
## [1] "Total Member Count"
## 
## attr(,"class")
## [1] "labels"

There are 2,799,938 members and 1,532,735 non-members, which is nearly one and a half times less. The 1,531,735 non-members are thr segment Cyclistic is seeking to convert into members. They comprise 34% of total rideship in 2023. Next we looked at at the bike count usage by member.

## visualizing member count by bike
ggplot(data = tripdata_2023) +
  geom_bar(aes(x = member_status, fill = bike_type, group = bike_type), alpha = 0.7, stat = "count", show.legend = TRUE) +
  geom_text(
    aes(x = member_status, label = after_stat(count), group = bike_type),
    stat = "count",
    position = position_stack(vjust = 0.75),
    size = 5,
    color = "black",
    caption = "Source: Cyclistic Bikes"
  ) +
  scale_y_continuous(labels = scales::label_number()) +
  scale_fill_manual(values = c( "yellow", "orange", "lightgreen")) +
  labs(title = "Member Count by Bike")
## Warning in geom_text(aes(x = member_status, label = after_stat(count), group =
## bike_type), : Ignoring unknown parameters: `caption`

Non-members prefer classic bikes over electric, using them 33% more than electric This suggests that speed is a factor in ridership. Next we looked at ride count by bike.

Curious about why the docked bikes ridership was so low while the average ride time is three to five times higher the non-members and member, a look a the data originator’s (https://divvybikes.com/system-data) website revealed that docked bikes refers to bikes that are broken and are, therefore “docked” until they are repaired. The average ride time is the time between when the bike was reported “broken” and the time it was repaired to be put back into circulation.

Thus, docked bikes, with an average ride time of 54 minutes, created outliers in the tripdata_2023 data frame, skewing the non-members average ride time. So docked bikes were filtered from the data frame.

## filtering out docked bikes
tripdata_2023v2 <- tripdata_2023 %>%
  filter(bike_type != "docked")

nrow(tripdata_2023v2)
## [1] 4255549

This reduced the data frame to 4,255,549 or 74.3% of the original 5,719,878. Thus, the tables representing the basic analyst questions continued.

ggplot(data = tripdata_2023v2) +
  geom_bar(aes(x = bike_type, fill = member_status, group = member_status), stat = "count", show.legend = TRUE) +
  geom_text(
    aes(x = bike_type, label = after_stat(count), group = member_status),
    stat = "count",
    position = position_stack(vjust = 0.75),
    size = 5,
    color = "black",
    caption = "Source: Cyclistic Bikes") +
  scale_y_continuous(labels = scales::label_number()) +
  scale_fill_manual(values = c( "lightblue", "pink"))+
  labs(title = "Ride Count by Bike")
## Warning in geom_text(aes(x = bike_type, label = after_stat(count), group =
## member_status), : Ignoring unknown parameters: `caption`

Next we asked questions that explored the ride time for both of the member types and their preferred bike_type:

+ What is the the total usage (ride time) for each member type?
+ What is the average usage (ride time) for each member type?
+ What is the the total usage (ride time) for each bike?
+ What is the average usage (ride time) for each bike?
## total ride time by member
tot_mem_time <- tripdata_2023v2 %>%
  group_by(member_status)%>%
  summarize(total_mem_time = sum(ride_time_mins, na.rm = TRUE))

# Plotting chart for total ride by member
ggplot(data = tot_mem_time, aes(x = member_status, y = total_mem_time, fill = member_status)) +
  geom_bar(stat = "identity",  alpha = 0.7) +
  geom_text(aes(label = total_mem_time),
            position = position_stack(vjust = 0.75),
            color = "black", size = 4) +  
  labs(title = "Total Ride Time by Member Type", x = "Member Type", y = "Ride Time (minutes)",
       caption = "Source: Cyclistic Bikes") +
  scale_fill_manual(values = c("lightblue", "pink"))+
  scale_y_continuous(labels = scales::label_number())

## average ride time by member
avg_mem_time <- tripdata_2023v2 %>%
  group_by(member_status) %>%
  summarize(avg_mem_mins = round(mean(ride_time_mins, na.rm = TRUE)))

# Plotting chart for average ride by member
ggplot(data = avg_mem_time, aes(x = member_status, y = avg_mem_mins, fill = member_status)) +
  geom_bar(stat = "identity",  alpha = 0.7) +
  geom_text(aes(label = avg_mem_mins),
            position = position_stack(vjust = 0.75),  
            color = "black", size = 4) +  
  labs(title = "Average Ride Time by Member Type",
       caption = "Source: Cyclistic Bikes") +
  scale_fill_manual(values = c( "lightblue", "pink"))+
  scale_y_continuous(labels = scales::label_number())

## bikes by total ride time
tot_bike_time = tripdata_2023v2 %>%
  group_by(bike_type) %>%
  summarize(total_ride_time_mins = sum(ride_time_mins, na.rm = TRUE)) 

# Plotting the bar chart
ggplot(data = tot_bike_time, aes(x = bike_type, y = total_ride_time_mins, fill = bike_type)) +
  geom_bar(stat = "identity",  alpha = 0.7) +
  geom_text(aes(label = total_ride_time_mins),
            position = position_stack(vjust = 0.75),  
            color = "black", size = 4) + 
  labs(title = "Total Ride Time by bike Type",x = "Type Bike", y = "Ride time (min)",
       caption = "Source: Cyclistic Bikes") +
  scale_fill_manual(values = c( "yellow", "lightgreen"))+
  scale_y_continuous(labels = scales::label_number())

## bikes by average ride time
avg_bike_time = tripdata_2023v2 %>%
  group_by(bike_type) %>%  
  summarize(avg_ride_time_mins = round(mean(ride_time_mins, na.rm = TRUE)))

# Plotting the bar chart
ggplot(data = avg_bike_time, aes(x = bike_type, y = avg_ride_time_mins, fill = bike_type)) +
  geom_bar(stat = "identity",  alpha = 0.7) +
  geom_text(aes(label = avg_ride_time_mins),
            position = position_stack(vjust = 0.75),
            color = "black", size = 4) +
  labs(title = "Average Ride Time by bike Type", x = "Bike Type", y = "Average ride (min)",
       caption = "Source: Cyclistic Bikes") +
  scale_fill_manual(values = c( "yellow", "lightgreen"))

  scale_y_continuous(labels = scales::label_number())
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --    1

Finally, I looked at the average ride time by member status and bike type.

# Calculate average ride time by bike type and member status
avg_ride_time <- tripdata_2023v2 %>%
  group_by(bike_type, member_status) %>%
  summarize(avg_ride_time_mins = round(mean(ride_time_mins, na.rm = TRUE)))
## `summarise()` has grouped output by 'bike_type'. You can override using the
## `.groups` argument.
# visualizing the grouped bar chart with totals
ggplot(data = avg_ride_time, aes(x = bike_type, y = avg_ride_time_mins, fill = member_status)) +
  geom_bar(stat = "identity", position = "dodge", color = "black", alpha = 0.7) +
  geom_text(aes(label = avg_ride_time_mins),
            position = position_dodge(width = 0.9),
            vjust = 1.0,  
            size = 5) +  
  labs(title = "Average Ride Time Minutes by Bike Type and Member Status", x = "Bike Type", y = "Average Minutes",
       caption = "Source: Cyclistic Bikes") +
  scale_y_continuous(labels = scales::label_number())+
  scale_fill_manual(values = c( "lightblue", "pink"))  

The graph suggests non-members are taking longer rides on both bike types.I then turned my attention to the difference in seasonality between members and non-members.

tripdata_2023v3 <- tripdata_2023v2 

tripdata_2023v3$s_month <- factor(tripdata_2023v3$s_month, levels = c("01", "02", "03", "04","05","06","07","08","09","10","11","12"))

mem_by_month = tripdata_2023v3  %>%
  group_by(s_month) %>%
  count(member_status)
 
# visualizing a graph of monthly ridership
ggplot(mem_by_month, aes(x = s_month, y = n, group = member_status, fill = member_status)) +
 geom_bar(data = mem_by_month, aes(x = s_month, y = n, fill = member_status), stat = "identity", position = "dodge", alpha = 0.7) +
 labs(title = "Member Count by Month",
       x = "Month",
       y = "Member Count",
      caption = "Source: Cyclistic Bikes") +
  scale_x_discrete(labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))+
  scale_fill_manual(values = c("member" = "lightblue", "non-member" = "pink")) +
  scale_y_continuous(labels = scales::label_number())

## monthly ridership by bike
bike_by_month = tripdata_2023v3  %>%
  group_by(s_month) %>%
  count(bike_type)

# creating a line graph
ggplot(bike_by_month, aes(x = s_month, y = n, group = bike_type, color = bike_type)) +
  geom_line() +
  geom_point() +
  labs(title = "Bike Count by Month",
       x = "Month",
       y = "Bike Count",
       caption = "Source: Cyclistic Bikes") +
  scale_x_discrete(labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))+
  scale_color_manual(values = c("electric" = "green", "classic" = "orange")) +
  scale_y_continuous(labels = scales::label_number())

# combining the plots
ggplot() +
  # Bar chart for bike types
  geom_bar(data = mem_by_month, aes(x = s_month, y = n, fill = member_status), stat = "identity", position = "dodge", alpha = 0.7) +
  # Line chart for member statuses
  geom_line(data = bike_by_month, aes(x = s_month, y = n, color = bike_type, group = bike_type), size = 1) +
  geom_point(data = bike_by_month, aes(x = s_month, y = n, color = bike_type, group = bike_type), size = 2, color = "black") +
  labs(title = "Member and Bikes by Month",
       x = "Month",
       y = "Count (Thousands)",
       caption = "Source: Cyclistic Bikes") +
  scale_x_discrete(labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
  scale_fill_manual(values = c("member" = "lightblue", "non-member" = "pink"))+
  scale_color_manual(values = c("electric" = "green", "classic" = "orange"))+
  scale_y_continuous(labels = scales::label_number())
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Usage peaks for electric bikes in June and classic bike in August. Ultimately, the usage for both bike types is consistently high throughout the summer months. Next we asked questions that looked at the seasonal behavior of each rider type and their preferred bike type:

+ What is the member participation by season?
## member count by season in order

tripdata_2023v3 <- as.data.frame(tripdata_2023v3)

# Set levels for the 'seasons' column
tripdata_2023v3$seasons <- factor(tripdata_2023v3$seasons, levels = c("Spring", "Summer", "Fall", "Winter"))

# Now, you can perform grouping and counting
mem_by_season <- tripdata_2023v3 %>%
  group_by(member_status, seasons) %>%
  count(member_status)

# Creating the plot
ggplot(data = mem_by_season, aes(x = seasons, y = n, fill = member_status)) +
  geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
  geom_text(aes(label = n),
            position = position_dodge(width = 1.0),
            vjust = -0.2,  
            size = 4) +
  labs(title = "Members by Season", x = "Seasons", y = "Count") +
  scale_y_continuous(labels = scales::label_number()) +
  scale_fill_manual(values = c("member" = "lightblue", "non-member" = "pink"))

## Set levels for the 'seasons' column
tripdata_2023v3$seasons <- factor(tripdata_2023v3$seasons, levels = c("Spring", "Summer", "Fall", "Winter"))


## average ride time by season
avg_ride_season <- tripdata_2023v3 %>%
  group_by(seasons, member_status) %>%
  summarize(avg_ride_seasons = round(mean(ride_time_mins, na.rm = TRUE)))
## `summarise()` has grouped output by 'seasons'. You can override using the
## `.groups` argument.
# Creating the plot
ggplot(data = avg_ride_season, aes(x = seasons, y = avg_ride_seasons, fill = member_status)) +
  geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
   geom_text(aes(label = avg_ride_seasons),
            position = position_dodge(width = 0.9),
            vjust = 1.0,  
            color = "black", size = 5) +
  labs(title = "Average Ride by Season",
       caption = "Source: Cyclistic Bikes") +
  scale_y_continuous(labels = scales::label_number()) +
  scale_fill_manual(values = c("member" = "lightblue", "non-member" = "pink"))

Looks like the marketing campaign should focus on the spring, summer, and fall months to convert non-members into members. Now that we have a better idea of when and how members and non-members use Cyclistic, let’s look at where the 2 groups tend to travel. We will answer a basic question:

+ What are the top ten routes for each member type?

This question is plotted to visualize the similarities and differences of the type of travel the non-members make in relation to members. First i created the data frame top_10_stations that was listed in descending order.

## finding top ten routes
top_10_stations <- tripdata_2023v3 %>%
  group_by(start_station, end_station, start_lat, start_lng, end_lat, end_lng)%>%
  summarise(top_10_count = n_distinct(ride_id), .groups = "drop")%>%
  arrange(desc(top_10_count)) %>%
  slice_head(n = 10)


print(top_10_stations)
## # A tibble: 10 × 7
##    start_station    end_station start_lat start_lng end_lat end_lng top_10_count
##    <chr>            <chr>           <dbl>     <dbl>   <dbl>   <dbl>        <int>
##  1 Streeter Dr & G… Streeter D…      41.9     -87.6    41.9   -87.6         9063
##  2 Ellis Ave & 60t… Ellis Ave …      41.8     -87.6    41.8   -87.6         6948
##  3 Ellis Ave & 60t… University…      41.8     -87.6    41.8   -87.6         6646
##  4 DuSable Lake Sh… DuSable La…      41.9     -87.6    41.9   -87.6         6616
##  5 Ellis Ave & 55t… Ellis Ave …      41.8     -87.6    41.8   -87.6         6389
##  6 University Ave … Ellis Ave …      41.8     -87.6    41.8   -87.6         6239
##  7 Calumet Ave & 3… State St &…      41.8     -87.6    41.8   -87.6         5468
##  8 State St & 33rd… Calumet Av…      41.8     -87.6    41.8   -87.6         5371
##  9 Michigan Ave & … Michigan A…      41.9     -87.6    41.9   -87.6         4800
## 10 DuSable Lake Sh… Streeter D…      41.9     -87.6    41.9   -87.6         4463

There are to total of 62,003 that comprises the top ten stations. I proceeded to map each station.This involved using Google Maps, then representing that count on a map of Chicago. (This is beyond what we learned in the Google Data Analytics course so i leaned heavily on Google and R documentation as well as AI searches to create the plots.)

## filtering for the top 10 start and end stations
top_10_stations <- tripdata_2023v3 %>%
  group_by(start_station, start_id, start_lat, start_lng, end_station, end_id, end_lat, end_lng, member_status) %>%
  filter(complete.cases(start_station, start_lat, start_lng, end_station, end_lat, end_lng)) %>%
  summarise(top_10_count = n_distinct(ride_id),
            .groups = "drop") %>%
  arrange(desc(top_10_count)) %>%
  slice_head(n = 10)


print(top_10_stations)
## # A tibble: 10 × 10
##    start_station start_id start_lat start_lng end_station end_id end_lat end_lng
##    <chr>         <chr>        <dbl>     <dbl> <chr>       <chr>    <dbl>   <dbl>
##  1 Streeter Dr … 13022         41.9     -87.6 Streeter D… 13022     41.9   -87.6
##  2 DuSable Lake… 13300         41.9     -87.6 DuSable La… 13300     41.9   -87.6
##  3 Calumet Ave … 13217         41.8     -87.6 State St &… 13216     41.8   -87.6
##  4 Ellis Ave & … KA15030…      41.8     -87.6 University… KA150…    41.8   -87.6
##  5 State St & 3… 13216         41.8     -87.6 Calumet Av… 13217     41.8   -87.6
##  6 University A… KA15030…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  7 Ellis Ave & … KA15030…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  8 Ellis Ave & … KA15040…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  9 DuSable Lake… 13300         41.9     -87.6 Streeter D… 13022     41.9   -87.6
## 10 Michigan Ave… 13042         41.9     -87.6 Michigan A… 13042     41.9   -87.6
## # ℹ 2 more variables: member_status <chr>, top_10_count <int>
## api key "AIzaSyAZk80X40yh8OPN3ri2C4hm7jhaIDnn78g"
## setting map
register_google(key = "AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg", write = TRUE)
## ℹ Replacing old key (AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg) with new key in C:\Users\admin\Documents/.Renviron
geocode("chicago, illnois", urlonly = TRUE)
## [1] "https://maps.googleapis.com/maps/api/geocode/json?address=chicago,+illnois&key=xxx"
ggmap_hide_api_key()
## ℹ ggmap will now suppress private api keys in the console.
## Loading map
map <- get_googlemap(center = c(lng = mean(top_10_stations$start_lng),
                                lat = mean(top_10_stations$start_lat)),
                     zoom = 12,
                     scale = 2,
                     style = "feature:all|element:labels|visibility:off",
                     color = "color",
                     apiKey = "AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg")
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=41.837,-87.612&zoom=12&size=640x640&scale=2&maptype=terrain&style=feature:all%7Celement:labels%7Cvisibility:off&key=xxx>
## Plot the map and add points for the top 10 start stations
ggmap(map) +
  geom_point(data = top_10_stations, 
             aes(x = start_lng, y = start_lat, size = top_10_count),
             na.rm = TRUE,
             color = "lightgreen", 
             alpha = 0.7) +
  geom_text(data = top_10_stations,
            aes(x = start_lng, 
                y = start_lat,
                label = top_10_count), 
            na.rm = TRUE,
            vjust = 1.5, 
            size = 3, 
            color = "black") +
  scale_size_continuous(range = c(3, 10)) +
  labs(title = "Cyclistic Top 10 Start Stations, Chicago",
       subtitle = "Based on the number of rides",
       caption = "Source: Google Maps",
       x = "Longitude",
       y = "Latitude",) +
  coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

## Loading map
map <- get_googlemap(center = c(lng = mean(top_10_stations$end_lng),
                                lat = mean(top_10_stations$end_lat)),
                     zoom = 12,
                     scale = 2,
                     style = "feature:all|element:labels|visibility:off",
                     color = "color",
                     apiKey = "AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg")
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=41.838,-87.611&zoom=12&size=640x640&scale=2&maptype=terrain&style=feature:all%7Celement:labels%7Cvisibility:off&key=xxx>
## Plot the map and add points for the top 10 end stations
ggmap(map) +
  geom_point(data = top_10_stations, 
             aes(x = end_lng, 
                y = end_lat, 
                size = top_10_count),
                na.rm = TRUE,
                color = "pink", alpha = 0.7) +
  geom_text(data = top_10_stations,
            aes(x = end_lng, 
                y = end_lat,
                label = top_10_count),
            na.rm = TRUE, 
            vjust = 1.5, 
            size = 3, 
            color = "black") +
  scale_size_continuous(range = c(3, 10)) + 
  labs(title = "Cyclistic Top 10 End Stations, Chicago",
       subtitle = "Based on the number of rides",
       x = "Longitude",
       y = "Latitude",
       caption = "Source: Google Maps") +
  coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

We can see that rides are concentrated in 3 areas of the city but the labels are difficult to read. After a search, i found the package ggrepel will address the text issue so i installed and loaded it.

## installing packages to fix overlapping labels
install.packages("ggrepel")
## Installing package into 'C:/Users/admin/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'ggrepel' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'ggrepel'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\admin\AppData\Local\R\win-library\4.3\00LOCK\ggrepel\libs\x64\ggrepel.dll
## to C:\Users\admin\AppData\Local\R\win-library\4.3\ggrepel\libs\x64\ggrepel.dll:
## Permission denied
## Warning: restored 'ggrepel'
## 
## The downloaded binary packages are in
##  C:\Users\admin\AppData\Local\Temp\RtmpoR59AD\downloaded_packages
library(ggrepel)

## revising map with better labels

top_10_stations_2 <- tripdata_2023v3 %>%
  group_by(start_station, start_id, start_lat, start_lng, end_station, end_id, end_lat, end_lng, member_status) %>%
  filter(complete.cases(start_station, start_id, start_lat, start_lng, end_station, end_id, end_lat, end_lng)) %>%
  summarise(top_10_count = n_distinct(ride_id),
            .groups = "drop") %>%
  arrange(desc(top_10_count)) %>%
  slice_head(n = 10)


print(top_10_stations_2)
## # A tibble: 10 × 10
##    start_station start_id start_lat start_lng end_station end_id end_lat end_lng
##    <chr>         <chr>        <dbl>     <dbl> <chr>       <chr>    <dbl>   <dbl>
##  1 Streeter Dr … 13022         41.9     -87.6 Streeter D… 13022     41.9   -87.6
##  2 DuSable Lake… 13300         41.9     -87.6 DuSable La… 13300     41.9   -87.6
##  3 Calumet Ave … 13217         41.8     -87.6 State St &… 13216     41.8   -87.6
##  4 Ellis Ave & … KA15030…      41.8     -87.6 University… KA150…    41.8   -87.6
##  5 State St & 3… 13216         41.8     -87.6 Calumet Av… 13217     41.8   -87.6
##  6 University A… KA15030…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  7 Ellis Ave & … KA15030…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  8 Ellis Ave & … KA15040…      41.8     -87.6 Ellis Ave … KA150…    41.8   -87.6
##  9 DuSable Lake… 13300         41.9     -87.6 Streeter D… 13022     41.9   -87.6
## 10 Michigan Ave… 13042         41.9     -87.6 Michigan A… 13042     41.9   -87.6
## # ℹ 2 more variables: member_status <chr>, top_10_count <int>
## api key "AIzaSyAZk80X40yh8OPN3ri2C4hm7jhaIDnn78g"

## setting map

register_google(key = "AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg", write = TRUE)
## ℹ Replacing old key (AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg) with new key in C:\Users\admin\Documents/.Renviron
geocode("Chicago, Illinois", urlonly = TRUE)
## [1] "https://maps.googleapis.com/maps/api/geocode/json?address=Chicago,+Illinois&key=xxx"
ggmap_hide_api_key()
## ℹ ggmap will now suppress private api keys in the console.
## Loading map

map <- get_map(location = "Chicago", zoom = 12, 
              style = "feature:all|element:labels|visibility:off",
              color = "bw",
              source = "google", key = api_key)
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=Chicago&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Chicago&key=xxx>
# Plot the map and add points for the top 10 start stations
ggmap(map, size = 1800, 1400) +
  geom_point(data = top_10_stations_2, 
             aes(x = start_lng, 
                 y = start_lat, 
                 size = top_10_count), 
             color = "lightgreen", 
             alpha = 0.9) +
  geom_text_repel(data = top_10_stations_2,
                  aes(x = start_lng,
                      y = start_lat,
                      label = top_10_count),
                  vjust = 0.5, 
                  hjust = 0.5,
                  na.rm = TRUE, 
                  box.padding = 0.5,
                  point.padding = 0.2,
                  force = 6,
                  size = 4,
                  color = "black",
                  max.overlaps = 50) +
  scale_size_continuous(range = c(3, 10)) +
  labs(title = "Cyclistic Top 10 Start Stations, Chicago",
       subtitle = "Based on the number of rides",
       caption = "Source: Google Maps")+
       coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

## Loading map

map <- get_map(location = "Chicago", zoom = 12, 
              style = "feature:all|element:labels|visibility:off",
              color = "bw",
              source = "google", 
              key = api_key)
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=Chicago&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Chicago&key=xxx>
# Plot the map and add points for the top 10 end stations

ggmap(map, size = 1800, 1400) +
  geom_point(data = top_10_stations_2, 
             aes(x = end_lng, 
                 y = end_lat, 
                 size = top_10_count), 
                 color = "pink", alpha = 0.9) +
  geom_text_repel(data = top_10_stations_2,
                  aes(x = start_lng,
                      y = start_lat,
                      label = top_10_count),
                  vjust = 0.5, 
                  hjust = 0.5,
                  na.rm = TRUE, 
                  box.padding = 0.5,
                  point.padding = 0.2,
                  force = 6,
                  size = 4,
                  color = "black",
                  max.overlaps = 50) +
  scale_size_continuous(range = c(3, 10)) +
  labs(title = "Cyclistic Top 10 End Stations, Chicago",
       subtitle = "Based on the number of rides",
       caption = "Source: Google Maps")+
       coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

The concentration became more apparent with better labeling. The majority of the rides are concentrated to 3 areas of the city. Also, there is no discernible difference between the general location of start and end stations. Thus, I decided to focus on start stations. Next, we looked at the members and non-members disbursement in the city.

## Share

library(ggplot2)
library(ggmap)

## map of start stations by member status
ggmap(map, size = 1800, 1400) +
  geom_point(data = top_10_stations_2, 
             aes(x = start_lng, 
                 y = start_lat, 
                 size = top_10_count), 
             color = "orange", 
             alpha = 0.7) +
  geom_text_repel(data = top_10_stations_2, 
                  aes(x = start_lng, 
                      y = start_lat, 
                      label = top_10_count),
                  vjust = 0.5, 
                  hjust = 0.5,
                  box.padding = 0.5, 
                  point.padding = 0.2, 
                  force = 6,
                  size = 4, 
                  color = "black") +
  scale_size_continuous(range = c(3, 10)) +
  labs(title = "Cyclistic Top 10 Start Stations by Member Type, Chicago",
       subtitle = "Based on the number of rides",
       x = "Longitude",
       y = "Latitude",
       caption = "Source: Google Maps") +
  facet_wrap(~ member_status)+
   coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

Members are primarily concentrated in the center of the city while the non-members are concentrated in on the Lake Shore of the city. I wondered what the non-members may been drawn to specifically in that part of the city.

So, I looked up the main tourist attractions in Chicago to see if there is a correlation between the top ten stations and those attractions. I looked up code to get the latitude and longitude of the addresses of those sites. My final question to answer is:

+ Where are those stations located in relations to popular landmarks and tourist attractions?

I found a list of the top ten tourist attractions with their addresses in Chicago then mapped them:

  1. Navy Pier
  2. Millennium Park
  3. Art Institute of Chicago
  4. Willis Tower
  5. Magnificent Mile
  6. Lincoln Park Zoo
  7. Wrigley Field
  8. 360 Chicago Observation Deck
  9. Museum of Science and Industry
  10. Shedd Aquarium
api_key <- 'AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg'

## data frame with the addresses
site_addr <- data.frame(
  Attraction = c("Navy Pier", "Millennium Park", "Art Institute of Chicago", "Willis Tower",
                 "Magnificent Mile", "Lincoln Park Zoo", "Wrigley Field",
                 "360 Chicago Observation Deck", "Museum of Science and Industry", "Shedd Aquarium"),
  Address = c("600 E Grand Ave, Chicago, IL 60611", "201 E Randolph St, Chicago, IL 60602",
              "111 S Michigan Ave, Chicago, IL 60603", "233 S Wacker Dr, Chicago, IL 60606",
              "Along N Michigan Ave, Chicago, IL 60611", "2001 N Clark St, Chicago, IL 60614",
              "1060 W Addison St, Chicago, IL 60613", "875 N Michigan Ave, Chicago, IL 60611",
              "5700 S Lake Shore Dr, Chicago, IL 60637", "1200 S Lake Shore Dr, Chicago, IL 60605")
)

# geocoding addresses to get latitude and longitude
geocoded <- geocode(site_addr$Address, source = "google", key = api_key)
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=600+E+Grand+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=201+E+Randolph+St,+Chicago,+IL+60602&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=111+S+Michigan+Ave,+Chicago,+IL+60603&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=233+S+Wacker+Dr,+Chicago,+IL+60606&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Along+N+Michigan+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=2001+N+Clark+St,+Chicago,+IL+60614&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=1060+W+Addison+St,+Chicago,+IL+60613&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=875+N+Michigan+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=5700+S+Lake+Shore+Dr,+Chicago,+IL+60637&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=1200+S+Lake+Shore+Dr,+Chicago,+IL+60605&key=xxx>
# adding the geocoded information to the original data frame
site_addr <- cbind(site_addr, geocoded)

# getting the map for Chicago
map <- get_map(location = "Chicago", zoom = 12, 
              style = "feature:all|element:labels|visibility:off",
              color = "bw",
              source = "google", key = api_key)
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=Chicago&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Chicago&key=xxx>
## plotting attractions on the road map
ggmap(map, size = 1300, 1100) +
  geom_point(data = site_addr, aes(x = lon, y = lat), shape = 24, fill = "red", size = 3) +
  geom_text_repel(data = site_addr, 
                  aes(x = lon, 
                      y = lat, 
                      label = Attraction),
                  vjust = 0.5, 
                  hjust = 0.5,
                  box.padding = 0.7, 
                  point.padding = 0.2, 
                  force = 10,
                  size = 3, 
                  color = "purple") +
  labs(title = "Attractions in Chicago", 
       subtitle = "Top ten tourist attractions",
       x = "Chicago", y = "",
       caption = "Source: Google Maps") +
  coord_cartesian(clip = "off")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

Then I combined the start stations with the tourist attractions to map the correlation.

# Replace 'YOUR_API_KEY' with your actual Google Maps API key
api_key <- 'AIzaSyCbvyWRjmOJUaebX5sL1YuxamtkP2oQixg'

# Data frame with the attractions
site_addr <- data.frame(
  Attraction = c("Navy Pier", "Millennium Park", "Art Institute of Chicago", "Willis Tower",
                 "Magnificent Mile", "Lincoln Park Zoo", "Wrigley Field",
                 "360 Chicago Observation Deck", "Museum of Science and Industry", "Shedd Aquarium"),
  Address = c("600 E Grand Ave, Chicago, IL 60611", "201 E Randolph St, Chicago, IL 60602",
              "111 S Michigan Ave, Chicago, IL 60603", "233 S Wacker Dr, Chicago, IL 60606",
              "Along N Michigan Ave, Chicago, IL 60611", "2001 N Clark St, Chicago, IL 60614",
              "1060 W Addison St, Chicago, IL 60613", "875 N Michigan Ave, Chicago, IL 60611",
              "5700 S Lake Shore Dr, Chicago, IL 60637", "1200 S Lake Shore Dr, Chicago, IL 60605")
)

# Geocode attractions
site_addr <- mutate(site_addr, location = geocode(Address, source = "google", key = api_key))
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=600+E+Grand+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=201+E+Randolph+St,+Chicago,+IL+60602&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=111+S+Michigan+Ave,+Chicago,+IL+60603&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=233+S+Wacker+Dr,+Chicago,+IL+60606&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Along+N+Michigan+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=2001+N+Clark+St,+Chicago,+IL+60614&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=1060+W+Addison+St,+Chicago,+IL+60613&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=875+N+Michigan+Ave,+Chicago,+IL+60611&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=5700+S+Lake+Shore+Dr,+Chicago,+IL+60637&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=1200+S+Lake+Shore+Dr,+Chicago,+IL+60605&key=xxx>
# Data frame with top 10 start stations
top_10_start_stations_2 <- tripdata_2023v3 %>%
  group_by(start_station, start_lat, start_lng, end_station, end_lat, end_lng, member_status) %>%
  summarise(top_10_count = n_distinct(ride_id), .groups = "drop") %>%
  filter(complete.cases(start_station, start_lat, start_lng)) %>%
  arrange(desc(top_10_count)) %>%
    slice_head(n = 10)



# Get the map for Chicago
map <- get_map(location = "Chicago", zoom = 11, 
               style = "feature:all|element:labels|visibility:off",
               color = "bw",
               source = "google", key = api_key)
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=Chicago&zoom=11&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Chicago&key=xxx>
# Plot attractions and top 10 start stations on the same map
ggmap(map, size = c(1800, 1400)) +
  geom_point(data = top_10_stations_2, aes(x = start_lng, y = start_lat, size = top_10_count), color = "orange", alpha = 0.9) +
  geom_point(data = site_addr, aes(x = location$lon, y = location$lat), shape = 24, fill = "red", size = 2) +
  geom_text(data = top_10_stations_2, 
                  aes(x = start_lng, 
                      y = start_lat, 
                      label = ""),
                  vjust = 0.5, 
                  hjust = 0.75,
                  box.padding = 0.5, 
                  point.padding = 0.1, 
                  force = 2,
                  size = 2, 
                  color = "pink") +
  scale_size_continuous(range = c(3, 10)) +
  labs(title = "Top 10 Attractions and Cyclistic Start Stations, Chicago",
       color = "Top 10 Count") +
   geom_text_repel(data = site_addr, 
                  aes(x = location$lon, 
                      y = location$lat, 
                      label = Attraction),
                  vjust = 0.5, 
                  hjust = 0.75,
                  box.padding = 0.7, 
                  point.padding = 0.2, 
                  force = 10,
                  size = 3.25, 
                  color = "purple",
                  max.overlaps = 40) +
  labs(subtitle = "Top ten tourist attractions",
       x = "Members versus Non-Members by location", y = "",
       caption = "Source: Google Maps") +
  coord_cartesian(clip = "off") +
  facet_wrap(~ member_status)
## Warning in geom_text(data = top_10_stations_2, aes(x = start_lng, y =
## start_lat, : Ignoring unknown parameters: `box.padding`, `point.padding`, and
## `force`
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

  1. Navy Pier
  2. Millennium Park
  3. Art Institute of Chicago
  4. Willis Tower
  5. Magnificent Mile
  6. Lincoln Park Zoo
  7. Wrigley Field
  8. 360 Chicago Observation Deck
  9. Museum of Science and Industry
  10. Shedd Aquarium

## ACT

###Recommendations As we can see, only one of the 10 major tourist attractions is located in the two area members are most likely to ride in the city center where as seven of the tourist sites are located near the Lake Michigan shore where non-members are most likely to ride. Thus, a marketing campaign partnering with city attractions to attach membership to tourist attraction access with a discounts for Cyclistic “tourist” memberships coupled with discounts for admission to those seven tourist attractions would likely yield positive results. This campaigns should be focused March through October, peaking in the summer months.

Furthermore, the longer ride average is possibly related to non-members using Cyclistic as a means of leisurely transportation to those sites to experience them. Thus, discount can be given on length of rides, that is, rewarding riders the longer they are on the bike, which is a habit that appears in the data for non-members.

Finally, a digital marketing campaign to encourage classic bike usage within the area of tourist attractions on social media would require an increase of classic bike availability but may also encourage more classic bike usage, the bike non-members seem to prefer.

Final Thoughts

To drill into the data, demographic information about each rider would be helpful to segment the non-member population further and glean more insights to target for marketing.