-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhotels script.Rmd
130 lines (110 loc) · 4.15 KB
/
hotels script.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
title: "Hotel Bookings"
author: "EE"
date: "2/13/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gghighlight)
library(ggtext)
library(sysfonts)
library(showtext)
library(countrycode)
library(lubridate)
set.seed(0408)
hotels <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv')
countries <- codelist_panel %>%
select(country.name.en, iso3c, continent)
background_col <- "#251e3e"
text_col <- "grey90"
min_var <- "#a3307e"
max_var <- "#fea873"
font_add_google("Lobster", "Lobster")
showtext_auto()
```
```{r data manipulation}
euro_hotels_ranked <- hotels %>%
mutate(quarter = case_when(
is.element(arrival_date_month, c("January", "February", "March")) ~ "Q1",
is.element(arrival_date_month, c("April", "May", "June")) ~ "Q2",
is.element(arrival_date_month, c("July", "August", "September")) ~ "Q3",
is.element(arrival_date_month, c("October", "November", "December")) ~ "Q4"
),
yr_qtr = paste0(arrival_date_year, ":", quarter) %>%
yq()) %>%
count(country, arrival_date_year, quarter, yr_qtr, sort = TRUE) %>%
left_join(countries, by = c("country" = "iso3c")) %>%
filter(continent == "Europe") %>%
distinct(country, yr_qtr, n, .keep_all = TRUE) %>%
add_count(country, name = "cnt") %>%
filter(cnt == 9) %>%
group_by(yr_qtr) %>%
mutate(rank = row_number(desc(n))) %>%
ungroup() %>%
group_by(country) %>%
mutate(sd_rank = sd(rank)) %>%
ungroup() %>%
mutate(line_col = case_when(
is.element(sd_rank, max(sd_rank)) ~ max_var,
is.element(sd_rank, min(sd_rank)) ~ min_var,
TRUE ~ "grey90"
))
```
```{r plot}
col_tbl <- euro_hotels_ranked %>%
distinct(country.name.en, line_col)
cols <- col_tbl$line_col
names(cols) <- col_tbl$country.name.en
hotel_plot <- euro_hotels_ranked %>%
ggplot(aes(x = yr_qtr, y = rank, color = country.name.en)) +
geom_point(size = 2, alpha = .8) +
geom_line(size = 1.5) +
geom_text(data = euro_hotels_ranked %>%
filter(yr_qtr == "2015-07-01"), aes(x = yr_qtr - 10, label = country.name.en, color = country.name.en),
hjust = 1,
family = "Lobster",
size = 14) +
geom_text(data = euro_hotels_ranked %>%
filter(yr_qtr == "2017-07-01"), aes(x = yr_qtr + 10, label = country.name.en, color = country.name.en),
hjust = 0,
family = "Lobster",
size = 14) +
scale_y_reverse() +
scale_x_date(
breaks = as.Date(c("2015-01-01", "2016-01-01", "2017-01-01")),
labels = c("", "2016", "2017")
) +
scale_color_manual(
values = cols
) +
labs(
x = "",
y = "Rank",
title = "Ranking European Countries by Number of Hotel Reservations",
subtitle = "For each fiscal quarter between July 2015 and July 2017, <span style='color:#a3307e'>Portugal</span> had more hotel reservations than any other country. Due to a spike in reservations in Q2 2016, <span style='color:#fea873'>Serbia</span> shows the most variability in its quarterly rankings.",
caption = "Data: Antonio, Almeida, & Nunes, 2019 | Viz: Eric Ekholm (@ekholm_e)"
) +
expand_limits(x = as.Date(c("2015-01-01", "2017-12-31"))) +
gghighlight(is.element(sd_rank, range(sd_rank)), use_direct_label = FALSE,
unhighlighted_params = list(color = text_col)) +
theme(
rect = element_rect(fill = background_col),
text = element_text(color = text_col, family = "Lobster", size = 24),
plot.title = element_markdown(size = 40),
plot.subtitle = element_textbox(size = 32, lineheight = 0),
panel.background = element_rect(fill = background_col),
plot.background = element_rect(fill = background_col),
panel.grid = element_blank(),
axis.text.x = element_text(color = text_col, size = 30),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
legend.position = "none",
plot.caption = element_markdown(size = 20)
)
x11()
print(hotel_plot)
ggsave(here::here("2020 - 7 - hotel bookings/hotel plot.jpg"), device = "jpeg", width = 7*1.5, height = 7)
```