##Firstly let`s install packages

library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'dplyr' was built under R version 4.3.1
## Warning: package 'stringr' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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(ggplot2)
library(lubridate)
library(readxl)
library(scales)
## Warning: package 'scales' was built under R version 4.3.1
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.1

##Create vector with colors

colors = c("#CC1011", "#665555", "#05a399", "#cfcaca", "#f5e840", "#0683c9", "#e075b0","black", "white", "pink", "blue", "green" )

##Lets upload our data

uber_apr_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-apr14.csv")
uber_may_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-may14.csv")
uber_jun_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-jun14.csv")
uber_jul_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-jul14.csv")
uber_aug_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-aug14.csv")
uber_sep_cs <- read.csv("/Users/oksanakovaluk/Downloads/uber-raw-data-sep14.csv")

##Lets unite our data

all_uber_data <- rbind(uber_apr_cs, uber_may_cs, uber_jun_cs, uber_jul_cs, uber_aug_cs, uber_sep_cs)

head(all_uber_data)
##          Date.Time     Lat      Lon   Base
## 1 4/1/2014 0:11:00 40.7690 -73.9549 B02512
## 2 4/1/2014 0:17:00 40.7267 -74.0345 B02512
## 3 4/1/2014 0:21:00 40.7316 -73.9873 B02512
## 4 4/1/2014 0:28:00 40.7588 -73.9776 B02512
## 5 4/1/2014 0:33:00 40.7594 -73.9722 B02512
## 6 4/1/2014 0:33:00 40.7383 -74.0403 B02512

##Also lets format dates in our datasets

all_uber_data$Date.Time <- as.POSIXct(all_uber_data$Date.Time, format="%m/%d/%Y %H:%M:%S")
all_uber_data$Time <- format(as.POSIXct(all_uber_data$Date.Time, format = "%m/%d/%Y %H:%M:%S"), format="%H:%M:%S")
all_uber_data$Date.Time <- ymd_hms(all_uber_data$Date.Time)
## Warning: 2211 failed to parse.
all_uber_data$day <- factor(day(all_uber_data$Date.Time))
all_uber_data$month <- factor(month(all_uber_data$Date.Time, label = TRUE))
all_uber_data$year <- factor(year(all_uber_data$Date.Time))
all_uber_data$dayofweek <- factor(wday(all_uber_data$Date.Time, label = TRUE))
all_uber_data$second = factor(second(hms(all_uber_data$Time)))
all_uber_data$minute = factor(minute(hms(all_uber_data$Time)))
all_uber_data$hour = factor(hour(hms(all_uber_data$Time)))
head(all_uber_data)
##             Date.Time     Lat      Lon   Base     Time day month year dayofweek
## 1 2014-04-01 00:11:00 40.7690 -73.9549 B02512 00:11:00   1   Apr 2014       Tue
## 2 2014-04-01 00:17:00 40.7267 -74.0345 B02512 00:17:00   1   Apr 2014       Tue
## 3 2014-04-01 00:21:00 40.7316 -73.9873 B02512 00:21:00   1   Apr 2014       Tue
## 4 2014-04-01 00:28:00 40.7588 -73.9776 B02512 00:28:00   1   Apr 2014       Tue
## 5 2014-04-01 00:33:00 40.7594 -73.9722 B02512 00:33:00   1   Apr 2014       Tue
## 6 2014-04-01 00:33:00 40.7383 -74.0403 B02512 00:33:00   1   Apr 2014       Tue
##   second minute hour
## 1      0     11    0
## 2      0     17    0
## 3      0     21    0
## 4      0     28    0
## 5      0     33    0
## 6      0     33    0
hourly_data <- all_uber_data %>% 
  na.omit(NA) %>% 
  group_by(hour) %>% 
  dplyr::summarize(Total = n())

##Lets visualize our hourly data

ggplot(hourly_data, aes(hour, Total)) + 
  geom_bar(stat="identity", 
           fill="steelblue", 
           color="red") + 
  ggtitle("Trips Every Hour", subtitle = "aggregated today") + 
  theme(legend.position = "none", 
        plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5)) + 
  scale_y_continuous(labels=comma)

month_hour_data <- all_uber_data %>% 
  na.omit(NA) %>% 
  group_by(month, hour) %>%  
  dplyr::summarize(Total = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.

##Lets visualize our monthly(hour) data

ggplot(month_hour_data, aes(hour, Total, fill=month)) + 
  geom_bar(stat = "identity") + 
  ggtitle("Trips by Hour and Month") + 
  scale_y_continuous(labels = comma)

day_data <- all_uber_data %>% 
  group_by(day) %>% 
  na.omit(NA) %>% 
  dplyr::summarize(Trips = n())

##Lets visualize our day data

ggplot(day_data, aes(day, Trips)) +
  geom_bar(stat = "identity") +
  ggtitle("Trips by day ") +
  theme(legend.position = "none") +
  scale_y_continuous(labels = comma) +
  scale_fill_manual(values = colors)

day_month_data <- all_uber_data %>% 
  na.omit(NA) %>% 
  group_by(dayofweek, month) %>% 
  dplyr::summarize(Trips = n())
## `summarise()` has grouped output by 'dayofweek'. You can override using the
## `.groups` argument.

##Lets visualize day month data filled by month

ggplot(day_month_data, aes(dayofweek, Trips, fill = month)) + 
  geom_bar(stat = "identity", aes(fill = month), position = "dodge") + 
  ggtitle("Trias by Day and Month") + 
  scale_y_continuous(labels = comma) + 
  scale_fill_manual(values = colors)

month_data <- all_uber_data %>% 
  na.omit(NA) %>% 
  group_by(month) %>% 
  dplyr::summarize(Total = n())
day_hour_data <- all_uber_data %>% 
  na.omit(NA) %>% 
  group_by(day, hour) %>% 
  dplyr::summarize(Total = n())
## `summarise()` has grouped output by 'day'. You can override using the `.groups`
## argument.

##Lets visualize day, hour filled by total

ggplot(day_hour_data, aes(day, hour, fill = Total)) + 
  geom_tile(color = "white") + 
  ggtitle("Heat Map by Hour and Day")

month_day_data <- all_uber_data %>% na.omit(NA) %>% group_by(month, day) %>% dplyr::summarize(Trips = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
ggplot(month_day_data, aes(day, month, fill = Trips)) +
  geom_tile(color = "white") +
  ggtitle("first myself viz")

month_dayweek_data <- all_uber_data %>%  na.omit(NA) %>%  group_by(month, dayofweek) %>%  dplyr::summarize(Trips = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
ggplot(month_dayweek_data, aes(dayofweek, month, fill = Trips)) +
  geom_tile(color = "white") +
  ggtitle("last one")