library(tidyverse)
library(lubridate)
library(viridis)
library(hexbin)
library(scales)Hotel Bookings
Setup
Load Packages
Read in Data
booking <- read_csv("booking.csv")Rows: 36285 Columns: 17
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): Booking_ID, type of meal, room type, market segment type, date of ...
dbl (11): number of adults, number of children, number of weekend nights, nu...
ℹ 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.
Modifying Column Names
booking <- booking %>%
rename(
adults = `number of adults`,
children = `number of children`,
weekend_nights = `number of weekend nights`,
week_nights = `number of week nights`,
meal = `type of meal`,
parking = `car parking space`,
room = `room type`,
segment = `market segment type`,
previous_cancellations = `P-C`,
previous_not_canceled = `P-not-C`,
price = `average price`,
requests = `special requests`,
reservation_date = `date of reservation`,
status = `booking status`,
lead_time = `lead time`
)booking <- booking %>%
mutate(
date = mdy(reservation_date),
canceled = status == "Canceled",
total_nights = weekend_nights + week_nights,
party_size = adults + children
)Warning: There was 1 warning in `mutate()`.
ℹ In argument: `date = mdy(reservation_date)`.
Caused by warning:
! 37 failed to parse.
Visualization 1: The Cancellation Cliff
Purpose
This visualization examines how lead time (how far in advance a room is booked) affects the likelihood that a booking will be canceled. Understanding this relationship can help hotels identify high-risk reservations and improve forecasting.
risk_curve <- booking |>
filter(lead_time <= quantile(lead_time, .99)) |>
mutate(
lead_band = cut(
lead_time,
breaks = seq(0, 350, by = 14),
include.lowest = TRUE
)
) |>
group_by(segment, lead_band) |>
summarise(
lead_mid = median(lead_time),
cancellation_rate = mean(canceled),
n = n(),
.groups = "drop"
) |>
filter(n >= 25)
ggplot(risk_curve,
aes(lead_mid,
cancellation_rate,
color = segment)) +
geom_line(aes(linewidth = sqrt(n)), alpha = .85) +
geom_point(aes(size = n), alpha = .8) +
scale_y_continuous(
labels = scales::percent_format()
) +
scale_color_viridis_d() +
labs(
title = "The Cancellation Cliff",
x = "Days Booked in Advance",
y = "Cancellation Rate",
color = "Market Segment"
)Interpretation
The visualization demonstrates a strong relationship between lead time and cancellations. As customers book further in advance, cancellation rates increase substantially. The effect is especially noticeable after approximately 90 days. Online reservations appear more sensitive to long lead times than other booking channels. This suggests that lead time is one of the strongest predictors of cancellation behavior in the dataset.
Visualization 2: Calendar of Cancellation Risk
Purpose
Most projects only show cancellation rates by month. This calendar heatmap provides a more detailed view by examining cancellation behavior across reservation dates throughout the year.
calendar <- booking |>
filter(!is.na(date)) |>
group_by(date) |>
summarise(
bookings = n(),
cancellation_rate = mean(canceled),
.groups = "drop"
) |>
mutate(
year = lubridate::year(date),
week = lubridate::isoweek(date),
weekday = factor(
lubridate::wday(
date,
label = TRUE,
week_start = 1
)
)
)
ggplot(calendar,
aes(week,
weekday,
fill = cancellation_rate,
alpha = bookings)) +
geom_tile(color = "white") +
facet_wrap(~year, ncol = 1) +
scale_fill_viridis_c(option = "magma") +
labs(
title = "Calendar of Cancellation Risk",
fill = "Cancellation Rate"
)Interpretation
Darker colors identify reservation dates associated with higher cancellation rates. The transparency level represents booking volume, allowing both demand and cancellation behavior to be viewed simultaneously. This layout makes it easier to identify periods where cancellations cluster, which may reflect seasonality, holidays, or booking patterns.
Visualization 3: Price-Planning Risk Landscape
Purpose
This visualization investigates how pricing and lead time interact to influence cancellation behavior. Rather than plotting thousands of overlapping points, a hexbin chart summarizes patterns across the entire dataset.
ggplot(
booking |> filter(
lead_time <= quantile(lead_time, .99),
price > 0,
price <= quantile(price, .99)
),
aes(
lead_time,
price,
z = as.numeric(canceled)
)
) +
stat_summary_hex(
fun = mean,
bins = 32
) +
scale_fill_viridis_c(
option = "plasma"
) +
labs(
title = "Price–Planning Risk Landscape",
x = "Lead Time",
y = "Average Price",
fill = "Cancellation Rate"
)Interpretation
Each hexagon represents a group of bookings with similar prices and lead times. Color intensity reflects the percentage of canceled reservations. The visualization reveals regions of elevated cancellation risk and allows comparison of booking behavior across different pricing levels. This chart highlights interactions between multiple variables that would be difficult to observe in simpler visualizations.
Visualization 4: Special Requests as a Cancellation Shield
Purpose
This visualization explores whether customers who submit special requests are more committed to their reservations and therefore less likely to cancel.
request_matrix <- booking |>
mutate(
request_group = case_when(
requests >= 3 ~ "3+",
TRUE ~ as.character(requests)
)
) |>
group_by(segment, request_group) |>
summarise(
rate = mean(canceled),
bookings = n(),
.groups = "drop"
)
ggplot(
request_matrix,
aes(
request_group,
segment
)
) +
geom_point(
aes(
size = bookings,
color = rate
)
) +
scale_color_viridis_c(
direction = -1
) +
labs(
title = "Special Requests as a Cancellation Shield",
x = "Number of Special Requests",
y = "Market Segment",
color = "Cancellation Rate",
size = "Bookings"
)Interpretation
The visualization suggests that guests who make special requests tend to cancel less frequently than those who make none. Customers willing to specify room preferences or accommodations may be more committed to their travel plans. This relationship appears across multiple market segments, making special requests a useful indicator of reservation reliability.
Key Findings
- Lead time is the strongest predictor of cancellation behavior.
- Online bookings exhibit higher cancellation risk than several other market segments.
- Price and lead time interact to create distinct areas of elevated cancellation risk.
- Customers submitting special requests generally appear more committed to their bookings.
- Reservation dates display periods of concentrated cancellation activity, suggesting seasonal effects.