-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotels-forcats.Rmd
48 lines (42 loc) · 2.34 KB
/
hotels-forcats.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
title: "Hotel bookings - factors"
author: "Mine Çetinkaya-Rundel, adapted by Lukas Jürgensmeier"
output: html_document
editor_options:
chunk_output_type: console
---
```{r load-pkg, message = FALSE}
library(tidyverse)
library(skimr)
```
```{r load-data, message = FALSE}
# From TidyTuesday: https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-02-11/readme.md
hotels <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv")
```
First, knit the document and view the following visualisation.
How are the months ordered?
What would be a better order?
Then, reorder the months on the x-axis (levels of `arrival_date_month`) in a way that makes more sense.
You will want to use a function from the **forcats** package, see <https://forcats.tidyverse.org/reference/index.html> for inspiration and help.
*Hint:* There is a built-in vector with the names of all twelve months --- `month.name`.
You could relevel the factor for the x-axis by this vector.
**Stretch goal:** If you finish the above task before time is up, change the y-axis label so the values are shown with dollar signs, e.g. \$80 instead of 80.
You will want to use a function from the **scales** package, see <https://scales.r-lib.org/reference/index.html> for inspiration and help.
```{r plot, fig.width=10}
hotels %>%
group_by(hotel, arrival_date_month) %>% # group by hotel type and arrival month
summarise(mean_adr = mean(adr)) %>% # calculate mean adr for each group
ggplot(aes(
x = arrival_date_month, # x-axis = arrival_date_month
y = mean_adr, # y-axis = mean_adr calculated above
group = hotel, # group lines by hotel type
color = hotel) # and color by hotel type
) +
geom_line() + # use lines to represent data
theme_minimal() + # use a minimal theme
labs(x = "Arrival month", # customize labels
y = "Mean ADR (average daily rate)",
title = "Comparison of resort and city hotel prices across months",
subtitle = "Resort hotel prices soar in the summer while ciry hotel prices remain relatively constant throughout the year",
color = "Hotel type")
```