-
Notifications
You must be signed in to change notification settings - Fork 4
/
workshop_jpm.Rmd
223 lines (185 loc) · 6.08 KB
/
workshop_jpm.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: "Makeover JPMorgan Timeline"
author: "Cedric Scherer"
date: "`r Sys.Date()`" ## current date
output:
rmdformats::readthedown:
highlight: kate
code_folding: show
toc_depth: 3
toc_float: true
---
<style>
h1, h2, h3, h4, h5, h6, legend {
color:#28a87d;
}
#sidebar h2 {
background-color: #28a87d;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE,
fig.showtext = TRUE, retina = 1,
fig.width = 9, fig.height = 6)
```
## Install Packages
We are going to use a suite of packages. If those are not installed on your machine, please run the following code. `{ggplot2}` is part of the `{tidyverse}` package collection, tgther with other helpful packages for a data science workflow such as `{dplyr}`, `{tibble}`, `{tidyr}`, and `{readr}`.
```{r packages, eval=F}
#install.packages("ggplot2")
install.packages("tidverse")
install.packages("here")
install.packages("gghighlight")
```
---
## Import the Data
We can import the data with the `{readr}` package. One could alternatively use the base function `readRDS()`.
```{r data}
library(tidyverse)
spendings <- readr::read_rds(here::here("data", "data-jpm.Rds"))
```
---
## Explore the Raw Data
Let's have a look at the data set:
```{r data-glimpse}
tibble::glimpse(spendings)
```
**Story:** Compare changes in year-to-year spendings (`change`) by essential categories (`category`)
**Goal:** Show winners and losers of the COVID-19 epidemic
---
## The Original Plot
The original plot was created by the JPMorgan Chase Institute:
![](./img/orig_line_jpmorgan.png)
We can create a similar plot with `{ggplot2}`:
```{r plot-jitter}
## change default theme
theme_set(theme_minimal())
theme_update(
panel.grid.minor = element_blank(),
axis.line.x = element_line(color = "black"),
axis.ticks.x = element_line(color = "black"),
plot.title = element_text(hjust = .5, margin = margin(b = 10)),
plot.title.position = "plot",
plot.caption.position = "plot",
plot.margin = margin(rep(15, 4))
)
g <-
ggplot(data = spendings,
aes(x = id, y = change,
color = category)) +
## add line national emergency
geom_vline(xintercept = 10.85, color = "grey40", linetype = "dashed") +
## add baseline at zero
geom_hline(yintercept = 0, color = "grey30", size = .8) +
## ad connected timeseries
geom_line(size = .8) +
geom_point(size = 1.6) +
## add label national emergenecy
geom_text(
data = tibble(
date = 10.6,
change = 65,
label = "National emergency\ndeclared March 13"
),
aes(x = date, y = change,
label = label),
inherit.aes = F,
size = 2.8,
lineheight = .95,
hjust = 1
) +
## avoid clipping of points close to the border
coord_cartesian(clip = "off") +
## change default colors
rcartocolor::scale_color_carto_d(palette = "Prism") +
## modify axes and plot appearance
scale_x_continuous(
expand = c(.003, .003),
breaks = seq(1, 15, by = 2),
labels = c("Jan 4", "Jan 18", "Feb 1", "Feb 15", "Feb 29", "Mar 14", "Mar 28", "Apr 11")
) +
scale_y_continuous(
breaks = seq(-80, 60, by = 20),
labels = glue::glue("{seq(-80, 60, by = 20)}%")
) +
labs(x = NULL, y = "End of week",
color = NULL,
title = "Year-over-year percent change in spending by essential category",
caption = "Source: JPMorgan Chase Institute")
g
```
---
## Small Multiples
One solution to resolve the overplotting-spaghetti-mess: small multiples, called `facets` in `{ggplot2}`:
```{r plot-facet, fig.width = 15, fig.height = 10}
g +
facet_wrap(~ category) +
theme(legend.position = "none",
strip.text = element_text(size = 13, face = "bold"),
panel.spacing = unit(2, "lines"))
```
## Highlight Some Trends
```{r plot-highlight}
g +
gghighlight::gghighlight(
change[which(id == 15)] < -50,
use_direct_label = FALSE
) +
scale_color_viridis_d(option = "inferno", end = .8)
```
## The Final Plot
We can combine both approaches by plotting small multiples but adding the other categories in the background as grey lines. We also keep the "national emergency" line for all facets but remove redundant labels.
```{r plot-facet-highlight, fig.width = 15, fig.height = 10}
gg <-
ggplot(spendings, aes(id, change, color = category)) +
geom_point() +
geom_line(size = .8, alpha = .5) +
gghighlight::gghighlight(
use_direct_label = F,
unhighlighted_params = list(color = "grey70", size = .5)
) +
facet_wrap(~ category, ncol = 4, scales = "free_x") +
coord_cartesian(clip = "off") +
scale_x_continuous(
expand = c(.003, .003),
breaks = seq(1, 15, by = 2),
labels = c("Jan 4", "Jan 18", "Feb 1", "Feb 15", "Feb 29", "Mar 14", "Mar 28", "Apr 11")
) +
scale_y_continuous(
breaks = seq(-80, 60, by = 20),
labels = glue::glue("{seq(-80, 60, by = 20)}%")
) +
scale_color_viridis_d(option = "inferno", end = .8, begin = .1, guide = "none") +
labs(
x = "End of week", y = NULL,
title = "Year-over-year percent change in spending by essential category",
caption = "Source: JPMorgan Chase Institute | Makeover: Cedric Scherer, Frontpage Data"
)
gg +
geom_vline(xintercept = 10.85, color = "grey40", linetype = "dashed") +
geom_hline(yintercept = 0, color = "grey30", size = .8) +
geom_area(alpha = .1) +
geom_line(size = 1.2) +
geom_point(size = 1.8) +
geom_text(
data = tibble(
id = 10.6,
change = -60,
label = "National emergency\ndeclared March 13",
category = factor("Groceries", levels = levels(spendings$category))
),
aes(label = label),
color = "grey40",
lineheight = .95,
hjust = 1
) +
theme(panel.grid.major.x = element_blank(),
panel.spacing = unit(2, "lines"),
strip.text = element_text(size = 13, face = "bold"),
plot.title = element_text(size = 20, hjust = .5, margin = margin(b = 10)))
```
<details><summary>Session Info</summary>
```{r sessionInfo, echo = F}
Sys.time()
sessionInfo()
```
</details>