library(tidyverse)
library(ggplot2)
library(readxl)
library(lubridate)
library(here)
library(varitas)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 7
Challenge Overview
Today’s challenge is to:
- read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data (as needed, including sanity checks)
- mutate variables as needed (including sanity checks)
- Recreate at least two graphs from previous exercises, but introduce at least one additional dimension that you omitted before using ggplot functionality (color, shape, line, facet, etc) The goal is not to create unneeded chart ink (Tufte), but to concisely capture variation in additional dimensions that were collapsed in your earlier 2 or 3 dimensional graphs.
- Explain why you choose the specific graph type
- If you haven’t tried in previous weeks, work this week to make your graphs “publication” ready with titles, captions, and pretty axis labels and other viewer-friendly features
R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code. And anyone not familiar with Edward Tufte should check out his fantastic books and courses on data visualizaton.
(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.
- eggs ⭐
- abc_poll ⭐⭐
- australian_marriage ⭐⭐
- hotel_bookings ⭐⭐⭐
- air_bnb ⭐⭐⭐
- us_hh ⭐⭐⭐⭐
- faostat ⭐⭐⭐⭐⭐
<-read.csv("_data/hotel_bookings.csv") hotel_bookings_untidy
Briefly describe the data
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.
#removing NA from children guests column.
<- na.omit(hotel_bookings_untidy, children)
hotel_bookings_untidy
#renaming
colnames(hotel_bookings_untidy)[2] = "cancellation?"
#creating an arrival date column from the three existing date columns
<-hotel_bookings_untidy %>%
hotel_bookings_untidymutate(arrival_date = str_c(arrival_date_day_of_month, arrival_date_month, arrival_date_year, sep = "-")) %>%
select(-c(4:7))
#consolidating weekend and weekday nights stayed into one "nights stayed" column
<-hotel_bookings_untidy %>%
hotel_bookings_untidymutate("nights stayed" = stays_in_weekend_nights + stays_in_week_nights) %>%
select(-c(4:5))
#creating true/false observations in the columns using integer data 0/1 (cancellation? and is repeated guest)
<-hotel_bookings_untidy %>%
hotel_bookmutate("repeat_guest" = as.logical(is_repeated_guest), "cancellation?"= as.logical(`cancellation?`)) %>%
select(-c(11))
glimpse(hotel_book)
Rows: 119,386
Columns: 28
$ hotel <chr> "Resort Hotel", "Resort Hotel", "Resort…
$ `cancellation?` <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALS…
$ lead_time <int> 342, 737, 7, 13, 14, 14, 0, 9, 85, 75, …
$ adults <int> 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
$ children <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ babies <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ meal <chr> "BB", "BB", "BB", "BB", "BB", "BB", "BB…
$ country <chr> "PRT", "PRT", "GBR", "GBR", "GBR", "GBR…
$ market_segment <chr> "Direct", "Direct", "Direct", "Corporat…
$ distribution_channel <chr> "Direct", "Direct", "Direct", "Corporat…
$ previous_cancellations <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ previous_bookings_not_canceled <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ reserved_room_type <chr> "C", "C", "A", "A", "A", "A", "C", "C",…
$ assigned_room_type <chr> "C", "C", "C", "A", "A", "A", "C", "C",…
$ booking_changes <int> 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ deposit_type <chr> "No Deposit", "No Deposit", "No Deposit…
$ agent <chr> "NULL", "NULL", "NULL", "304", "240", "…
$ company <chr> "NULL", "NULL", "NULL", "NULL", "NULL",…
$ days_in_waiting_list <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ customer_type <chr> "Transient", "Transient", "Transient", …
$ adr <dbl> 0.00, 0.00, 75.00, 75.00, 98.00, 98.00,…
$ required_car_parking_spaces <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ total_of_special_requests <int> 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 3, …
$ reservation_status <chr> "Check-Out", "Check-Out", "Check-Out", …
$ reservation_status_date <chr> "2015-07-01", "2015-07-01", "2015-07-02…
$ arrival_date <chr> "1-July-2015", "1-July-2015", "1-July-2…
$ `nights stayed` <int> 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, …
$ repeat_guest <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALS…
#Now I have "hotel_book", which I'll use to start visualization.
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.
<-hotel_book %>%
reservation_outcomesgroup_by(reservation_status, hotel) %>%
summarise(n=n()) %>%
ungroup()
#Question: what specifically is summarise(n) grabbing from here?
glimpse(reservation_outcomes)
Rows: 6
Columns: 3
$ reservation_status <chr> "Canceled", "Canceled", "Check-Out", "Check-Out", "…
$ hotel <chr> "City Hotel", "Resort Hotel", "City Hotel", "Resort…
$ n <int> 32182, 10831, 46228, 28938, 916, 291
%>%
hotel_book summarise(n=n(reservation_status))
Error in `summarise()`:
ℹ In argument: `n = n(reservation_status)`.
Caused by error in `n()`:
! unused argument (reservation_status)
Visualization with Multiple Dimensions
I first attempted to plot this data as a line plot and then a bar plot but for some reason, the lines wouldn’t show up.
ggplot(reservation_outcomes, aes(hotel, n, color=reservation_status))+
geom_boxplot()+
labs(title="Reservation Outcomes by Hotel", x="Hotel Type", y="Reservations")
ggplot(hotel_book, aes(reservation_status, reserved_room_type))+
geom_count()+
facet_wrap(vars(hotel))+
labs(title="Reservation Outcomes by Room Type", x="Reservation Outcome", y="Room Type")+
theme(axis.text.x=element_text(angle=90))