Challenge 6

challenge_6
hotel_bookings
Visualizing Time and Relationships
Author

Calvin Cuff

Published

January 14, 2024

library(here)
library(readr)
library(tidyverse)
library(ggplot2)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Challenge Overview

Today’s challenge is to:

  1. read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
  2. tidy data (as needed, including sanity checks)
  3. mutate variables as needed (including sanity checks)
  4. create at least one graph including time (evolution)
  • try to make them “publication” ready (optional)
  • Explain why you choose the specific graph type
  1. Create at least one graph depicting part-whole relationships
  • By part whole, we just mean how one variable’s constituent parts differ (or don’t). For example, does the gender of computer programmers affect salary?
  • try to make them “publication” ready (optional)
  • Explain why you choose the specific graph type

R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code.

(be sure to only include the category tags for the data you use!)

Read in data

Read in one (or more) of the following datasets, using the correct R package and command. - hotel_bookings ⭐⭐⭐⭐

hotels <- read_csv(here("data", "hotel_bookings.csv"))
head(hotels)
# A tibble: 6 × 32
  hotel        is_canceled lead_time arrival_date_year arrival_date_month
  <chr>              <dbl>     <dbl>             <dbl> <chr>             
1 Resort Hotel           0       342              2015 July              
2 Resort Hotel           0       737              2015 July              
3 Resort Hotel           0         7              2015 July              
4 Resort Hotel           0        13              2015 July              
5 Resort Hotel           0        14              2015 July              
6 Resort Hotel           0        14              2015 July              
# ℹ 27 more variables: arrival_date_week_number <dbl>,
#   arrival_date_day_of_month <dbl>, stays_in_weekend_nights <dbl>,
#   stays_in_week_nights <dbl>, adults <dbl>, children <dbl>, babies <dbl>,
#   meal <chr>, country <chr>, market_segment <chr>,
#   distribution_channel <chr>, is_repeated_guest <dbl>,
#   previous_cancellations <dbl>, previous_bookings_not_canceled <dbl>,
#   reserved_room_type <chr>, assigned_room_type <chr>, …

Briefly describe the data

dim(hotels)
[1] 119390     32
colnames(hotels)
 [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"       
unique(hotels$arrival_date_year)
[1] 2015 2016 2017
unique(hotels$hotel)
[1] "Resort Hotel" "City Hotel"  
unique(hotels$country)
  [1] "PRT"  "GBR"  "USA"  "ESP"  "IRL"  "FRA"  "NULL" "ROU"  "NOR"  "OMN" 
 [11] "ARG"  "POL"  "DEU"  "BEL"  "CHE"  "CN"   "GRC"  "ITA"  "NLD"  "DNK" 
 [21] "RUS"  "SWE"  "AUS"  "EST"  "CZE"  "BRA"  "FIN"  "MOZ"  "BWA"  "LUX" 
 [31] "SVN"  "ALB"  "IND"  "CHN"  "MEX"  "MAR"  "UKR"  "SMR"  "LVA"  "PRI" 
 [41] "SRB"  "CHL"  "AUT"  "BLR"  "LTU"  "TUR"  "ZAF"  "AGO"  "ISR"  "CYM" 
 [51] "ZMB"  "CPV"  "ZWE"  "DZA"  "KOR"  "CRI"  "HUN"  "ARE"  "TUN"  "JAM" 
 [61] "HRV"  "HKG"  "IRN"  "GEO"  "AND"  "GIB"  "URY"  "JEY"  "CAF"  "CYP" 
 [71] "COL"  "GGY"  "KWT"  "NGA"  "MDV"  "VEN"  "SVK"  "FJI"  "KAZ"  "PAK" 
 [81] "IDN"  "LBN"  "PHL"  "SEN"  "SYC"  "AZE"  "BHR"  "NZL"  "THA"  "DOM" 
 [91] "MKD"  "MYS"  "ARM"  "JPN"  "LKA"  "CUB"  "CMR"  "BIH"  "MUS"  "COM" 
[101] "SUR"  "UGA"  "BGR"  "CIV"  "JOR"  "SYR"  "SGP"  "BDI"  "SAU"  "VNM" 
[111] "PLW"  "QAT"  "EGY"  "PER"  "MLT"  "MWI"  "ECU"  "MDG"  "ISL"  "UZB" 
[121] "NPL"  "BHS"  "MAC"  "TGO"  "TWN"  "DJI"  "STP"  "KNA"  "ETH"  "IRQ" 
[131] "HND"  "RWA"  "KHM"  "MCO"  "BGD"  "IMN"  "TJK"  "NIC"  "BEN"  "VGB" 
[141] "TZA"  "GAB"  "GHA"  "TMP"  "GLP"  "KEN"  "LIE"  "GNB"  "MNE"  "UMI" 
[151] "MYT"  "FRO"  "MMR"  "PAN"  "BFA"  "LBY"  "MLI"  "NAM"  "BOL"  "PRY" 
[161] "BRB"  "ABW"  "AIA"  "SLV"  "DMA"  "PYF"  "GUY"  "LCA"  "ATA"  "GTM" 
[171] "ASM"  "MRT"  "NCL"  "KIR"  "SDN"  "ATF"  "SLE"  "LAO" 

This data set captures 119390 hotel bookings from different countries in the years 2015, 2016 and 2017. There are 119390 rows and 32 columns describing detailed characteristics of the bookings such as the length of stay, details of the reservation, and the guest’s profile. There are only 2 unique hotels identified, as Resort Hotel and City Hotel, which accepted bookings from customers across 178 different countries.

Tidy Data (as needed)

Is your data already tidy, or is there work to be done? Be sure to anticipate your end result to provide a sanity check, and document your work here.

Are there any variables that require mutation to be usable in your analysis stream? For example, do you need to calculate new values in order to graph them? Can string values be represented numerically? Do you need to turn any variables into factors and reorder for ease of graphics and visualization?

Document your work here.

This data already comes in fairly tidy, but one thing we can do to cleanup is to make the hotel name a factor. Since we are also concerned with time studies here the dates can also be modified. The arrival date is spread among 3 separate columns for year, month, and day. The month is also the proper name and I would like it in numeric form. We will mutate the data frame to create a new column for arrival_date in the format of month-day-year, and sort accordingly.

I will also make 2 new data frames that will assist on visualizations later.

There are over 100,000 bookings in the 3 year period captured in this data. I am going to group the data by each date to get the corresponding average daily rate to reduce the number of data points. I will follow a similar procedure to group by arrival date and by hotel so that we can have a nice comparison of average daily rates across hotels.

hotels_tidy <- hotels %>%
  # Turn hotel into factor
  mutate(hotel=as.factor(hotel)) %>%
  # Turn month name into numeric representation
  mutate(arrival_date_month=match(arrival_date_month, month.name),
         # Join month-day-year and cast as Date
         arrival_date=  as.Date(str_c(arrival_date_month, arrival_date_day_of_month, arrival_date_year, sep= "-"),format="%m-%d-%Y")) %>%
  # Sort by date
  arrange(ymd(arrival_date))
  
hotels_by_day <- hotels_tidy%>%
  group_by(arrival_date) %>%
  summarise(avg_price = mean(adr)) %>%
  ungroup()

hotels_by_hotel <- hotels_tidy %>% 
  group_by(arrival_date, hotel) %>%
  summarise(avg_price = mean(adr)) %>%
  ungroup()

Time Dependent Visualization

Using the hotels_by_day data frame, we now have 1 data point per each day, so we can graph the evolution of hotel rates over time. This graph quickly shows that there seems to be a seasonal effect of hotel rates, where they rise over the summer months then dip again during winter months.

hotels_by_day %>%  
  ggplot(aes(arrival_date, avg_price)) + 
  geom_point() + 
  geom_smooth() +
  scale_x_date(limits=range(hotels_by_day$arrival_date), breaks="3 months", date_labels = "%m-%Y") +
  scale_y_continuous(limits = c(0, ceiling(max(hotels_by_day$avg_price)))) + 
  labs(x = "Arrival Date", y = "Average Daily Rate", title= "Hotel Average Daily Rate over Time")

Visualizing Part-Whole Relationships

A good non-time dependent visualization to compare hotel popularity would be to simply see which hotel has had more bookings during time period of the study. This bar graph shows that the City Hotel has nearly twice as many bookings than the Resort Hotel.

hotels_tidy %>%
  ggplot(aes(hotel, fill=hotel)) +
  geom_bar() + labs(x = "Hotel", y = "Number of Bookings", title = "Number of Bookings Across Hotels")

Using the hotels_by_hotel data frame, each row represents the average daily rate of a specific hotel and arrival date. We can graph the arrival date vs average daily rate similarly to the time dependent graph above, but now we can include separate path visualizations for the different hotels. This will add a different color to indicate the hotel and allows quick comparison of prices across hotels. The graph shows that both hotels seem to follow similar price fluctuations to the overall average from the above graph. However the City Resort price has much less seasonal fluctuation where the Resort Hotel sees drastic increases over the summer and declines over the winter months.

hotels_by_hotel %>%
  ggplot(aes(arrival_date, avg_price, col=hotel)) +
  geom_path() + 
  scale_x_date(limits=range(hotels_by_hotel$arrival_date), breaks="3 months", date_labels = "%m-%Y") +
  scale_y_continuous(limits = c(0, ceiling(max(hotels_by_hotel$avg_price)))) + 
  labs(x = "Arrival Date", y = "Average Daily Rate", title= "Comparison of Hotels Average Daily Rate over Time")