R Project - Uber Data Analysis Project
Posted by Superadmin on August 22 2020 04:17:54

Uber Data Analysis Project

BY  · UPDATED · FEBRUARY 19, 2020

Welcome to part 2 of R and Data Science Projects designed by DataFlair. In our series of R projects, we are trying to use all the concepts related to Machine learning, AI and Data Science.

We recommend you to follow all the steps given in the projects so that you will master the technology rapidly. In today’s R project, we will analyze the Uber Pickups in New York City dataset. This is more of a data visualization project that will guide you towards using the ggplot2 library for understanding the data and for developing an intuition for understanding the customers who avail the trips. So, before we start, take a quick revision to data visualization concepts.

R Data Science Project – Uber Data Analysis

Talking about our Uber data analysis project, data storytelling is an important component of Machine Learning through which companies are able to understand the background of various operations. With the help of visualization, companies can avail the benefit of understanding the complex data and gain insights that would help them to craft decisions. You will learn how to implement the ggplot2 on the Uber Pickups dataset and at the end, master the art of data visualization in R.

You can download the dataset utilized in this project here – Uber Dataset

1. Importing the Essential Packages

In the first step of our R project, we will import the essential packages that we will use in this uber data analysis project. Some of the important libraries of R that we will use are –

This is the backbone of this project. ggplot2 is the most popular data visualization library that is most widely used for creating aesthetic visualization plots.

This is more of an add-on to our main ggplot2 library. With this, we can create better create extra themes and scales with the mainstream ggplot2 package.

Our dataset involves various time-frames. In order to understand our data in separate time categories, we will make use of the lubridate package.

This package is the lingua franca of data manipulation in R.

This package will help you to tidy your data. The basic principle of tidyr is to tidy the columns where each variable is present in a column, each observation is represented by a row and each value depicts a cell.

With the help of this package, we will be able to interface with the JavaScript Library called – Datatables.

With the help of graphical scales, we can automatically map the data to the correct scales with well-placed axes and legends.

  1. library(ggplot2) 
  2.  library(ggthemes) 
  3.  library(lubridate) 
  4.  library(dplyr) 
  5.  library(tidyr) 
  6.  library(DT) 
  7.  library(scales)

Input Screenshot 1:

Importing Libraries
Input Screenshot 2:

Importing Libraries in R
Input Screenshot 3:

R import libraries

2. Creating vector of colors to be implemented in our plots

In this step of data science project, we will create a vector of our colors that will be included in our plotting functions. You can also select your own set of colors.

Code:

  1. colors = c(""#CC1011", "#665555", "#05a399", "#cfcaca", "#f5e840", "#0683c9", "#e075b0"")

Input Screenshot 4:

create vector of colors

3. Reading the Data into their designated variables

Now, we will read several csv files that contain the data from April 2014 to September 2014. We will store these in corresponding data frames like apr_data, may_data, etc. After we have read the files, we will combine all of this data into a single dataframe called ‘data_2014’.

To master this R Uber data analysis project, you need to know everything related to data frames in R

Then, in the next step, we will perform the appropriate formatting of Date.Time column. Then, we will proceed to create factors of time objects like day, month, year etc.

Code:

  1. apr_data <- read.csv("uber-raw-data-apr14.csv") 
  2. may_data <- read.csv("uber-raw-data-may14.csv") 
  3. jun_data <- read.csv("uber-raw-data-jun14.csv") 
  4. jul_data <- read.csv("uber-raw-data-jul14.csv") 
  5. aug_data <- read.csv("uber-raw-data-aug14.csv")
  6. sep_data <- read.csv("uber-raw-data-sep14.csv")
  7. data_2014 <- rbind(apr_data,may_data, jun_data, jul_data, aug_data, sep_data)
  8. data_2014$Date.Time <- as.POSIXct(data_2014$Date.Time, format = "%m/%d/%Y %H:%M:%S")
  9. data_2014$Time <- format(as.POSIXct(data_2014$Date.Time, format = "%m/%d/%Y %H:%M:%S"), format="%H:%M:%S")
  10. data_2014$Date.Time <- ymd_hms(data_2014$Date.Time)
  11. data_2014$day <- factor(day(data_2014$Date.Time))
  12. data_2014$month <- factor(month(data_2014$Date.Time, label = TRUE))
  13. data_2014$year <- factor(year(data_2014$Date.Time))
  14. data_2014$dayofweek <- factor(wday(data_2014$Date.Time, label = TRUE))

Input Screenshot 5:

reading the data

Code:

  1. data_2014$hour <- factor(hour(hms(data_2014$Time)))
  2. data_2014$minute <- factor(minute(hms(data_2014$Time)))
  3. data_2014$second <- factor(second(hms(data_2014$Time)))

Input Screenshot 6:

reading data

Plotting the trips by the hours in a day

In the next step or R project, we will use the ggplot function to plot the number of trips that the passengers had made in a day. We will also use dplyr to aggregate our data. In the resulting visualizations, we can understand how the number of passengers fares throughout the day. We observe that the number of trips are higher in the evening around 5:00 and 6:00 PM.

  1. hour_data <- data_2014 %>%
  2. group_by(hour) %>%
  3. dplyr::summarize(Total = n())
  4. datatable(hour_data)

Input Screenshot 7:

Plotting the trips

Output Screenshot:

uber data analysis

Code:

  1. ggplot(hour_data, aes(hour, Total)) +
  2. geom_bar( stat = "identity", fill = "steelblue", color = "red") +
  3. ggtitle("Trips Every Hour") +
  4. theme(legend.position = "none") +
  5. scale_y_continuous(labels = comma)
  6. month_hour <- data_2014 %>%
  7. group_by(month, hour) %>%
  8. dplyr::summarize(Total = n())
  9. ggplot(month_hour, aes(hour, Total, fill = month)) +
  10. geom_bar( stat = "identity") +
  11. ggtitle("Trips by Hour and Month") +
  12. scale_y_continuous(labels = comma)

Input Screenshot 8:

Trips every Hour Plot Code

Input Screenshot 9:

month_hour

Output:

uber data analysis

Output:

uber data analysis

Plotting data by trips during every day of the month

In this section of DataFlair R project, we will learn how to plot our data based on every day of the month. We observe from the resulting visualization that 30th of the month had the highest trips in the year which is mostly contributed by the month of April.

Code:

  1. day_group <- data_2014 %>%
  2. group_by(day) %>%
  3. dplyr::summarize(Total = n())
  4. datatable(day_group)

Output Screenshot:

uber data analysis

Code:

  1. ggplot(day_group, aes(day, Total)) +
  2. geom_bar( stat = "identity", fill = "steelblue") +
  3. ggtitle("Trips Every Day") +
  4. theme(legend.position = "none") +
  5. scale_y_continuous(labels = comma)

Input Screenshot 10:

plotting data by trips

Output:

uber data analysis

Code:

  1. day_month_group <- data_2014 %>%
  2. group_by(month, day) %>%
  3. dplyr::summarize(Total = n())
  4. ggplot(day_month_group, aes(day, Total, fill = month)) +
  5. geom_bar( stat = "identity") +
  6. ggtitle("Trips by Day and Month") +
  7. scale_y_continuous(labels = comma) +
  8. scale_fill_manual(values = colors)

Input Screenshot 11:

plotting data by trips

Output:

uber data analysis

Number of Trips taking place during months in a year

In this section, we will visualize the number of trips that are taking place each month of the year. In the output visualization, we observe that most trips were made during the month of September. Furthermore, we also obtain visual reports of the number of trips that were made on every day of the week.

Code:

  1. month_group <- data_2014 %>%
  2. group_by(month) %>%
  3. dplyr::summarize(Total = n())
  4. datatable(month_group)

Output Screenshot:uber data analysis

Code:

  1. ggplot( , aes(month, Total, fill = month)) +
  2. geom_bar( stat = "identity") +
  3. ggtitle("Trips by Month") +
  4. theme(legend.position = "none") +
  5. scale_y_continuous(labels = comma) +
  6. scale_fill_manual(values = colors)

Input Screenshot 12:

 Trips by Month Plot

Output:Uber data analysis

  1. month_weekday <- data_2014 %>%
  2. group_by(month, dayofweek) %>%
  3. dplyr::summarize(Total = n())
  4. ggplot(month_weekday, aes(month, Total, fill = dayofweek)) +
  5. geom_bar( stat = "identity", position = "dodge") +
  6. ggtitle("Trips by Day and Month") +
  7. scale_y_continuous(labels = comma) +
  8. scale_fill_manual(values = colors)

Input Screenshot 13:

day month group

Output:

uber data analysis

Finding out the number of Trips by bases

In the following visualization, we plot the number of trips that have been taken by the passengers from each of the bases. There are five bases in all out of which, we observe that B02617 had the highest number of trips. Furthermore, this base had the highest number of trips in the month B02617. Thursday observed highest trips in the three bases – B02598, B02617, B02682.

Code:

  1. ggplot(data_2014, aes(Base)) +
  2. geom_bar(fill = "darkred") +
  3. scale_y_continuous(labels = comma) +
  4. ggtitle("Trips by Bases")

Input Screenshot 14:

 Trips by Bases

Output:

uber data analysis

Code:

  1. ggplot(data_2014, aes(Base, fill = month)) +
  2. geom_bar(position = "dodge") +
  3. scale_y_continuous(labels = comma) +
  4. ggtitle("Trips by Bases and Month") +
  5. scale_fill_manual(values = colors)

Input Screenshot 15:

Trips by Bases and Months

Output:

uber data analysis

Code:

  1. ggplot(data_2014, aes(Base, fill = dayofweek)) +
  2. geom_bar(position = "dodge") +
  3. scale_y_continuous(labels = comma) +
  4. ggtitle("Trips by Bases and DayofWeek") +
  5. scale_fill_manual(values = colors)

Output:

uber data analysis

Creating a Heatmap visualization of day, hour and month

In this section, we will learn how to plot heatmaps using ggplot(). We will plot five heatmap plots –

Code:

  1. day_and_hour <- data_2014 %>%
  2. group_by(day, hour) %>%
  3. dplyr::summarize(Total = n())
  4. datatable(day_and_hour)

Input Screenshot 16:

day_hour_month code

Output Screenshot:

uber data analysis

Code:

  1. ggplot(day_and_hour, aes(day, hour, fill = Total)) +
  2. geom_tile(color = "white") +
  3. ggtitle("Heat Map by Hour and Day")

Input Screenshot 17:

creating heatmap visualization

Output:

Heat Map Hour and Day

Code:

  1. ggplot(day_month_group, aes(day, month, fill = Total)) +
  2. geom_tile(color = "white") +
  3. ggtitle("Heat Map by Month and Day")

Input Screenshot 18:

Heatmap Month and Day

Output:

Heat map by Month and Day

Code:

  1. ggplot(month_weekday, aes(dayofweek, month, fill = Total)) +
  2. geom_tile(color = "white") +
  3. ggtitle("Heat Map by Month and Day of Week")

Input Screenshot 19:

Heatmap Month and Day of Week

Output:

Month and Day of Week

Code:

  1. month_base <- data_2014 %>%
  2. group_by(Base, month) %>%
  3. dplyr::summarize(Total = n())
  4. day0fweek_bases <- data_2014 %>%
  5. group_by(Base, dayofweek) %>%
  6. dplyr::summarize(Total = n())
  7. ggplot(month_base, aes(Base, month, fill = Total)) +
  8. geom_tile(color = "white") +
  9. ggtitle("Heat Map by Month and Bases")

Input Screenshot 20:

Heatmap Month and Bases
Output:

Month and Bases Output

Code:

  1. ggplot(day0fweek_bases, aes(Base, dayofweek, fill = Total)) +
  2. geom_tile(color = "white") +
  3. ggtitle("Heat Map by Bases and Day of Week")

Input Screenshot 21:

Days and Base of Week

Output:

Bases and Days of Weeks

Creating a map visualization of rides in New York

In the final section, we will visualize the rides in New York city by creating a geo-plot that will help us to visualize the rides during 2014 (Apr – Sep) and by the bases in the same period.

Code:

  1. min_lat <- 40.5774
  2. max_lat <- 40.9176
  3. min_long <- -74.15
  4. max_long <- -73.7004
  5. ggplot(data_2014, aes(x=Lon, y=Lat)) +
  6. geom_point(size=1, color = "blue") +
  7. scale_x_continuous(limits=c(min_long, max_long)) +
  8. scale_y_continuous(limits=c(min_lat, max_lat)) +
  9. theme_map() +
  10. ggtitle("NYC MAP BASED ON UBER RIDES DURING 2014 (APR-SEP)")
  11. ggplot(data_2014, aes(x=Lon, y=Lat, color = Base)) +
  12. geom_point(size=1) +
  13. scale_x_continuous(limits=c(min_long, max_long)) +
  14. scale_y_continuous(limits=c(min_lat, max_lat)) +
  15. theme_map() +
  16. ggtitle("NYC MAP BASED ON UBER RIDES DURING 2014 (APR-SEP) by BASE")

Input Screenshot 22:

creating a map visualization
Output:

Creating a map visualization

Uber data analysis using R

Output:

Uber data analyis using R

Summary

At the end of the Uber data analysis R project, we observed how to create data visualizations. We made use of packages like ggplot2 that allowed us to plot various types of visualizations that pertained to several time-frames of the year. With this, we could conclude how time affected customer trips. Finally, we made a geo plot of New York that provided us with the details of how various users made trips from different bases.