Note: This Analysis was conducted according to a fictional scenario as a part of the Google Data Analytics Coursework.
The R programming language, for me, is incredibly convenient to use, in comparision to other analysis options as it does the things we would otherwise need multiple programs for and is hence, my go-to analysis tool.
This step-by-step documentation is meant to showcase my thought process using R.
The Dataset was provided as a part of the coursework.
Scenario
The stakeholder is interested in developing promotions based on different booking distributions, but first they need to know how many of the transactions are occurring for each different distribution type.
For this, a simple bar chart will work.
#import and skim through the dataset
hotel_bookings <- read.csv("~/hotel_bookings.csv")
colnames(hotel_bookings)## [1] "hotel" "is_canceled"
## [3] "lead_time" "arrival_date_year"
## [5] "arrival_date_month" "arrival_date_week_number"
## [7] "arrival_date_day_of_month" "stays_in_weekend_nights"
## [9] "stays_in_week_nights" "adults"
## [11] "children" "babies"
## [13] "meal" "country"
## [15] "market_segment" "distribution_channel"
## [17] "is_repeated_guest" "previous_cancellations"
## [19] "previous_bookings_not_canceled" "reserved_room_type"
## [21] "assigned_room_type" "booking_changes"
## [23] "deposit_type" "agent"
## [25] "company" "days_in_waiting_list"
## [27] "customer_type" "adr"
## [29] "required_car_parking_spaces" "total_of_special_requests"
## [31] "reservation_status" "reservation_status_date"
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── 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
The
stakeholder now has more questions. They want to know if the number of
bookings for each distribution type is different depending on whether or
not there was a deposit or what market segment they represent.
Modifying the code for the first chart;
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel,fill=deposit_type ))This second bar chart is similar to the previous chart, except that ‘market_segment’ data is being recorded in the color-coded sections of each bar.
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel,fill=market_segment ))After reviewing the new charts, the stakeholder requests to create separate charts for each deposit type and market segment to help them understand the differences more clearly.
Using facet;
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_wrap(~deposit_type )This code chunk creates three bar charts for ‘no_deposit’, non_refund’, and ‘refundable’ deposit types. It’s hard to read the x-axis labels here, so I added one piece of code at the end that rotates the text to 45 degrees to make it easier to read.
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_wrap(~deposit_type) +
theme(axis.text.x = element_text(angle = 45))Used the same syntax to create a different chart for each market segment:
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_wrap(~market_segment) +
theme(axis.text.x = element_text(angle = 45))The facet_grid function does something similar. The main
difference is that facet_grid will include plots even if
they are empty.
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_grid(~deposit_type) +
theme(axis.text.x = element_text(angle = 45))Notice that the ‘Refundable’ chart has much less data plotted than the other two. Now, I could put all of this in one chart and explore the differences by deposit type and market segment.
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_wrap(~deposit_type~market_segment) +
theme(axis.text.x = element_text(angle = 45))Next, the stakeholder wants to run a family-friendly promotion targeting key market segments. She wants to know which market segments generate the largest number of bookings, and where these bookings are made (city hotels or resort hotels).
First, I decide to create a bar chart showing each hotel type and market segment and the different colors represent each market segment.
After creating this bar chart, it seems difficult to compare the size of the market segments at the top of the bars so using the facet_wrap() function, I’ll create a separate plot for each market segment.
Considering all the data, the stakeholder decides to send the promotion to families that make online bookings for city hotels. The online segment is the fastest growing segment, and families tend to spend more at city hotels than other types of guests.
She asks if a plot can be created that shows the relationship between lead time and guests traveling with children for online bookings at city hotels. This will give her a better idea of the specific timing for the promotion.
Filtering the dataset;
onlineta_city_hotels <- hotel_bookings %>%
filter(hotel=="City Hotel") %>%
filter(market_segment=="Online TA")To view filtered dataset
Based on the previous filter, the scatterplot below shows data for online bookings for city hotels. The plot reveals that bookings with children tend to have a shorter lead time, and bookings with 3 children have a significantly shorter lead time (<200 days). So, promotions targeting families can be made closer to the valid booking dates.
## Warning: Removed 1 rows containing missing values (`geom_point()`).
The stakeholder says that they would like to share the visualization breaking down payment type by city because it will help inform how the company targets promotions in the future. They ask to create a cleaned and labeled version and save it as a .png file for them to include in a presentation.
In this visualization it is unclear where the data is from, what the
main takeaway is, or even what the data is showing. To explain all of
that, annotations in ggplot2 can be used.
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = market_segment)) +
facet_wrap(~hotel) +
labs(title="Market segment comparision based on hotel types for bookings")To add another detail about what time period this data covers, we
need to find out when the data is from. The min() and
‘’max()’’ functions could be used in the data as variables.
Next, we’ll add subtitles;
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = market_segment, fill=market_segment)) +
facet_wrap(~hotel) +
labs(title="Comparison of market segments by hotel type for hotel bookings",
subtitle=paste0("Data from: ", mindate, " to ", maxdate))This chart is displaying the technical details a little too
prominently and it shouldn’t be the second thing people notice during
the presentation so I decide to switch the subtitle to a
caption which will appear in the bottom right corner
instead.
m<-ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = market_segment,fill=market_segment)) +
facet_wrap(~hotel)
m +labs(title="Comparison of market segments by hotel type for hotel bookings",
caption=paste0("Data from: ", mindate, " to ", maxdate))Cleaning up the variables;
m +
theme(axis.text.x = element_text(angle = 45)) +
labs(title="Comparison of market segments by hotel type for hotel bookings",
caption=paste0("Data from: ", mindate, " to ", maxdate),
x="Market Segment",
y="Number of Bookings")To finally save;
CONCLUSION
Through this scenario, I cleaned, filtered, analyzed and showcased the task deliverables using R.