library(tidyverse)
library(ggplot2)
::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")
bookings
head(bookings)
# 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
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.
#Dropping empty value rows
<- bookings %>%
bookings drop_na()
#Converted Arrival Into a Single Date Column
<- bookings %>%
bookings mutate("arrival_date" = str_c(arrival_date_day_of_month,
arrival_date_month,sep="/"))
arrival_date_year, colnames(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"
[33] "arrival_date"
#new df with only necessary columns
<- bookings %>%
book select(hotel, lead_time, country, market_segment, distribution_channel, arrival_date, arrival_date_year)
head(book)
# A tibble: 6 × 7
hotel lead_time country market_segment distribution_channel arrival_date
<chr> <dbl> <chr> <chr> <chr> <chr>
1 Resort Hot… 342 PRT Direct Direct 1/July/2015
2 Resort Hot… 737 PRT Direct Direct 1/July/2015
3 Resort Hot… 7 GBR Direct Direct 1/July/2015
4 Resort Hot… 13 GBR Corporate Corporate 1/July/2015
5 Resort Hot… 14 GBR Online TA TA/TO 1/July/2015
6 Resort Hot… 14 GBR Online TA TA/TO 1/July/2015
# ℹ 1 more variable: arrival_date_year <dbl>
Visualization with Multiple Dimensions
ggplot(book, aes(country)) +
geom_bar() +
labs(title = "Bookings per Country")
ggplot(book, aes(arrival_date)) +
geom_bar() +
labs(title = "Bookings per Date")
ggplot(book, aes(arrival_date_year, fill = hotel)) +
geom_bar() +
labs(title = "Hotel Type By Year")
ggplot(book, aes(arrival_date_year, fill = market_segment)) +
geom_bar() +
labs(title = "Distribution for Different Type of Hotels for Different Years as per the Market Segment") +
facet_wrap(vars(hotel))