This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
forked from rsangole/tidytuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020-12-15 big mac.R
87 lines (81 loc) · 4.1 KB
/
2020-12-15 big mac.R
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
library(tidyverse)
library(ggplot2)
tuesdata <- tidytuesdayR::tt_load('2020-12-22')
bigmac <- tuesdata$`big-mac`
bigmac_us_dat <- bigmac %>%
dplyr::filter(currency_code == "USD") %>%
dplyr::select(date, bigmac_us_price_USD = local_price)
bigmac_value_dat <- bigmac %>%
dplyr::select(date,
currency_code,
name,
bigmac_local_price = local_price,
dollar_ex) %>%
dplyr::left_join(
y = bigmac_us_dat,
on = date
)
bigmac_value_dat <- bigmac_value_dat %>%
dplyr::mutate(
bigmac_ex_rate = bigmac_local_price / bigmac_us_price_USD,
valuation = 1 - bigmac_ex_rate / dollar_ex
)
bigmac_value_dat %>%
dplyr::select(date, name, valuation) %>%
dplyr::filter(date %in% as.Date(c("2020-07-01", "2015-07-01"))) %>%
tidyr::pivot_wider(names_from = date, values_from = valuation) %>%
janitor::clean_names() %>%
dplyr::mutate(movement = x2020_07_01 - x2015_07_01,
bw = ifelse(x2020_07_01 > x2015_07_01, "Valuation worse in 5 years", "Valuation better in 5 years"),
bw = ifelse(name == "United States", "ref", bw),
bw = factor(bw, levels = c("Valuation worse in 5 years", "Valuation better in 5 years", "ref")),
name = forcats::fct_reorder(name, x2020_07_01),
label_pos = ifelse(bw == "Valuation worse in 5 years",
x2020_07_01 + 0.02,
x2020_07_01 - 0.02)) %>%
dplyr::arrange(movement) %>%
tidyr::drop_na()-> to_plot
to_plot %>%
ggplot(aes(y = name, color = bw)) +
geom_hline(yintercept = 3, color = "gray80", size = 0.3) +
geom_vline(xintercept = 0, color = "gray90", size = 0.2) +
geom_point(aes(x = x2015_07_01), color = "gray70", size = 1.5) +
geom_segment(aes(x = x2020_07_01, xend = x2015_07_01, yend = name), color = "gray70") +
geom_point(data = to_plot %>% filter(name != "United States"),
mapping = aes(x = x2020_07_01), size = 2) +
geom_text(data = to_plot %>% filter(bw == "Valuation worse in 5 years"),
mapping = aes(x = label_pos, y = name, label = name),
size = 3.5, hjust = 0, show.legend = FALSE) +
geom_text(data = to_plot %>% filter(bw == "Valuation better in 5 years"),
mapping = aes(x = label_pos, y = name, label = name),
size = 3.5, hjust = 1, show.legend = FALSE) +
geom_label(data = data.frame(x = c(-0.5, -0.25, 0, 0.25, 0.50, 0.75), y = c(3, 3, 3, 3, 3, 3)),
mapping = aes(x = x, y = y, label = scales::label_percent()(x)),
size = 2.8, color = "gray30", hjust = 0.5,
label.size = 0, label.padding = unit(0.2, "lines")) +
annotate(geom = "curve", xend = 0.674, yend = 42.5, x = 0.674 + 0.04, y = 44,
curvature = .3, arrow = arrow(length = unit(2, "mm")), color = "#e76f51") +
annotate(geom = "text", x = 0.674 + 0.04 + 0.006, y = 44,
label = "2020", hjust = "left", size = 3.4, color = "#e76f51") +
annotate(geom = "curve", xend = 0.563, yend = 42.5, x = 0.563 + 0.04, y = 44,
curvature = .3, arrow = arrow(length = unit(2, "mm")), color = "gray70") +
annotate(geom = "text", x = 0.563 + 0.04 + 0.006, y = 44,
label = "2015", hjust = "left", size = 3.4, color = "gray70") +
theme_minimal() +
theme(axis.text = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(color = "#264653")) +
theme(legend.position = c(0.14, 0.2),
legend.title = element_blank(),
legend.text = element_text(color = "#264653", size = 9.5)) +
scale_color_manual(values = c("#e76f51", "#2a9d8f")) +
scale_x_continuous(labels = scales::label_percent(),
breaks = c(-0.5, -0.25, 0, 0.25, 0.5, 0.75),
expand = c(0, 0.1)) +
scale_y_discrete(expand = c(-0.1, 7))+
annotate(geom = "text", x = -0.3, y = 44,
label = "Over Valued", size = 3.4, color = "#264653") +
annotate(geom = "text", x = 0.3, y = 44,
label = "Under Valued", size = 3.4, color = "#264653") +
labs(x = "Big Mac Exchange Rate Valuation", y = "")