Ohsee 2024-01-22
## [1] "C:/Users/HP/Documents/R_WORKSPACE"
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ 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
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
## Rows: 1640718 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): from_station_name, to_station_name, usertype, gender
## dbl (5): trip_id, bikeid, from_station_id, to_station_id, birthyear
## num (1): tripduration
## dttm (2): start_time, end_time
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## spc_tbl_ [1,640,718 × 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ trip_id : num [1:1640718] 23479388 23479389 23479390 23479391 23479392 ...
## $ start_time : POSIXct[1:1640718], format: "2019-07-01 00:00:27" "2019-07-01 00:01:16" ...
## $ end_time : POSIXct[1:1640718], format: "2019-07-01 00:20:41" "2019-07-01 00:18:44" ...
## $ bikeid : num [1:1640718] 3591 5353 6180 5540 6014 ...
## $ tripduration : num [1:1640718] 1214 1048 1554 1503 1213 ...
## $ from_station_id : num [1:1640718] 117 381 313 313 168 300 168 313 43 43 ...
## $ from_station_name: chr [1:1640718] "Wilton Ave & Belmont Ave" "Western Ave & Monroe St" "Lakeview Ave & Fullerton Pkwy" "Lakeview Ave & Fullerton Pkwy" ...
## $ to_station_id : num [1:1640718] 497 203 144 144 62 232 62 144 195 195 ...
## $ to_station_name : chr [1:1640718] "Kimball Ave & Belmont Ave" "Western Ave & 21st St" "Larrabee St & Webster Ave" "Larrabee St & Webster Ave" ...
## $ usertype : chr [1:1640718] "Subscriber" "Customer" "Customer" "Customer" ...
## $ gender : chr [1:1640718] "Male" NA NA NA ...
## $ birthyear : num [1:1640718] 1992 NA NA NA NA ...
## - attr(*, "spec")=
## .. cols(
## .. trip_id = col_double(),
## .. start_time = col_datetime(format = ""),
## .. end_time = col_datetime(format = ""),
## .. bikeid = col_double(),
## .. tripduration = col_number(),
## .. from_station_id = col_double(),
## .. from_station_name = col_character(),
## .. to_station_id = col_double(),
## .. to_station_name = col_character(),
## .. usertype = col_character(),
## .. gender = col_character(),
## .. birthyear = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
## Rows: 1,640,718
## Columns: 12
## $ trip_id <dbl> 23479388, 23479389, 23479390, 23479391, 23479392, 23…
## $ start_time <dttm> 2019-07-01 00:00:27, 2019-07-01 00:01:16, 2019-07-0…
## $ end_time <dttm> 2019-07-01 00:20:41, 2019-07-01 00:18:44, 2019-07-0…
## $ bikeid <dbl> 3591, 5353, 6180, 5540, 6014, 4941, 3770, 5442, 2957…
## $ tripduration <dbl> 1214, 1048, 1554, 1503, 1213, 310, 1248, 1550, 1583,…
## $ from_station_id <dbl> 117, 381, 313, 313, 168, 300, 168, 313, 43, 43, 511,…
## $ from_station_name <chr> "Wilton Ave & Belmont Ave", "Western Ave & Monroe St…
## $ to_station_id <dbl> 497, 203, 144, 144, 62, 232, 62, 144, 195, 195, 84, …
## $ to_station_name <chr> "Kimball Ave & Belmont Ave", "Western Ave & 21st St"…
## $ usertype <chr> "Subscriber", "Customer", "Customer", "Customer", "C…
## $ gender <chr> "Male", NA, NA, NA, NA, "Male", NA, NA, NA, NA, NA, …
## $ birthyear <dbl> 1992, NA, NA, NA, NA, 1990, NA, NA, NA, NA, NA, NA, …
Trips_q3$tripduration <- as.numeric(Trips_q3$tripduration)
iqr_value <- IQR(Trips_q3$tripduration)
lower_bound <- quantile(Trips_q3$tripduration, 0.25) - 1.5 * iqr_value
upper_bound <- quantile(Trips_q3$tripduration, 0.75) + 1.5 * iqr_value
Trips_q3_filtered <- Trips_q3 %>%
filter(tripduration >= lower_bound, tripduration <= upper_bound)## [1] 61
library(ggplot2)
ggplot(Trips_q3, aes(x = usertype, fill = gender)) +
geom_bar(position = "fill", stat = "count") +
scale_y_continuous(labels = scales::percent_format()) +
labs(title = "Percentage of Each Gender by User Type",
x = "User Type",
y = "Percentage of Gender") +
scale_fill_manual(values = c("Male" = "blue", "Female" = "pink", "Other" = "gray")) +
theme_minimal()# Drop rows with null values in 'gender'
Trips_q3_cleaned <- na.omit(Trips_q3[, c("gender", "usertype")])
# Calculate the percentage distribution of gender by user type
percentage_distribution <- Trips_q3_cleaned %>%
group_by(usertype, gender) %>%
summarise(count = n()) %>%
mutate(percentage = count / sum(count) * 100)## `summarise()` has grouped output by 'usertype'. You can override using the
## `.groups` argument.
# Create a bar plot for the distribution of gender by user type
ggplot(percentage_distribution, aes(x = gender, y = percentage, fill = gender)) +
geom_bar(position = "dodge", stat = "identity") +
facet_wrap(~ usertype, scales = "free_y") +
labs(title = "Distribution of Gender by User Type",
x = "Gender",
y = "Percentage",
fill = "Gender") +
scale_fill_manual(values = c("Male" = "skyblue", "Female" = "pink")) + # Adjust colors as needed
theme_minimal()## tibble [1,353,368 × 2] (S3: tbl_df/tbl/data.frame)
## $ gender : chr [1:1353368] "Male" "Male" "Male" "Male" ...
## $ usertype: chr [1:1353368] "Subscriber" "Subscriber" "Subscriber" "Subscriber" ...
## - attr(*, "na.action")= 'omit' Named int [1:287350] 2 3 4 5 7 8 9 10 11 12 ...
## ..- attr(*, "names")= chr [1:287350] "2" "3" "4" "5" ...
## [1] 150943.9
# Assuming 'Trips_q3' is your data frame
# Replace 'Trips_q3' with your actual data frame name
# Load the ggplot2 library if not loaded
# library(ggplot2)
# Create a scatter plot with trip duration in minutes
ggplot(data = Trips_q3) +
geom_point(mapping = aes(x = tripduration, y = usertype, color = usertype)) +
labs(title = "An Overview of The Trip Duration for Both User Types",
x = "Trip Duration (Minutes)",
y = "User Type") +
scale_color_manual(values = c("Subscriber" = "blue", "Customer" = "orange")) +
theme_minimal()Trips_q3$usertype <- factor(Trips_q3$usertype, levels = c("Subscriber", "Customer"))
Trips_q3$gender <- factor(Trips_q3$gender, levels = c("Male", "Female", "Other"))
# Create a bar plot with specified fill colors
ggplot(Trips_q3, aes(x = usertype, fill = usertype)) +
geom_bar() +
labs(title = "User Type Distribution",
x = "User Type",
y = "Count") +
scale_fill_manual(values = c("Subscriber" = "blue", "Customer" = "orange", "Other" = "gray")) +
theme_minimal()
#Create a boxplot to Show The Trip Durations For Both Subscribers And
Customers
ggplot(Trips_q3, aes(x = usertype, y = tripduration, color = usertype)) +
geom_boxplot() +
labs(title = "Trip Durations by User Type",
x = "User Type",
y = "Trip Duration (seconds)")+
scale_color_manual(values = c("Subscriber" = "blue", "Customer" = "orange", "Other" = "gray")) +
theme_minimal()ggplot(avg_trips, aes(x = usertype, y = avg_tripduration, fill = usertype)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Average Trip Duration by User Type",
x = "User Type",
y = "Average Trip Duration") +
scale_fill_manual(values = c("Subscriber" = "blue", "Customer" = "orange")) +
theme_minimal()ggplot(Trips_q3, aes(x = as.POSIXct(start_time), group = 1)) +
geom_line(stat = "count", binwidth = 60 * 60 * 24) +
labs(title = "Number of Trips Over Time",
x = "Start Time",
y = "Number of Trips")## Warning in geom_line(stat = "count", binwidth = 60 * 60 * 24): Ignoring unknown
## parameters: `binwidth`
Trips_q3$start_time <- as.POSIXct(Trips_q3$start_time)
Trips_q3$day_of_week <- weekdays(Trips_q3$start_time)
Trips_q3$month <- months(Trips_q3$start_time)
head(select(Trips_q3, start_time, day_of_week))## # A tibble: 6 × 2
## start_time day_of_week
## <dttm> <chr>
## 1 2019-07-01 00:00:27 Monday
## 2 2019-07-01 00:01:16 Monday
## 3 2019-07-01 00:01:48 Monday
## 4 2019-07-01 00:02:07 Monday
## 5 2019-07-01 00:02:13 Monday
## 6 2019-07-01 00:02:21 Monday
user_day_stat<- Trips_q3%>%
select(usertype,day_of_week)%>%
group_by(usertype,day_of_week)%>%
summarise(user_count = n())## `summarise()` has grouped output by 'usertype'. You can override using the
## `.groups` argument.
ggplot(data = user_day_stat) +
geom_point(mapping = aes(x = factor(day_of_week, levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")), y = user_count, color = usertype, size = user_count)) +
labs(title = "Number of Trips by User Type and Day of the Week",
x = "Day of the Week",
y = "Number of Trips",
color = "User Type") +
theme_minimal() +
scale_x_discrete(labels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) +
scale_color_manual(values = c("Subscriber" = "blue", "Customer" = "orange")) +
scale_size_continuous(range = c(3, 10)) # Create a bar plot
ggplot(Trips_q3, aes(x = factor(day_of_week, levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")), y = tripduration/60, fill = usertype)) +
geom_bar(stat = "summary", fun = "mean", position = "dodge") +
labs(title = "Average Trip Duration by Weekday and User Type",
x = "Weekday",
y = "Average Trip Duration (Minutes)",
fill = "User Type") +
scale_x_discrete(labels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) +
scale_fill_manual(values = c("Subscriber" = "blue", "Customer" = "orange")) +
theme_minimal()# Assuming 'your_data' is your data frame
# Replace 'your_data' with your actual data frame name
# Load the ggplot2 library if not loaded## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 61 465 813 1742 1460 9056633
# Calculate the percentage of round trips for each user type
round_trip_percentage <- Trips_q3 %>%
group_by(usertype) %>%
summarise(round_trip_percentage = sum(from_station_name == to_station_name) / n() * 100)
# Create a bar plot to visualize round trip percentages with specified colors
ggplot(round_trip_percentage, aes(x = usertype, y = round_trip_percentage, fill = usertype)) +
geom_bar(stat = "identity", color = "black") +
scale_fill_manual(values = c("Subscriber" = "blue", "Customer" = "orange")) +
labs(title = "Percentage of Round Trips by User Type",
x = "User Type",
y = "Percentage of Round Trips") +
scale_y_continuous(labels = scales::percent_format(scale = 1)) +
theme_minimal()