-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_sets.qmd
317 lines (256 loc) · 7.8 KB
/
parallel_sets.qmd
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Parallel sets {#sec-parallel-sets}
```{r}
#| label: setup
#| message: false
#| warning: false
#| include: false
library(tidyverse)
library(lubridate)
library(scales)
library(knitr)
library(kableExtra)
library(colorblindr)
library(downlit)
# fonts ----
library(extrafont)
library(sysfonts)
source("_common.R")
# use font
showtext::showtext_auto()
# set theme
ggplot2::theme_set(theme_ggp2g(
base_size = 12))
# pak::pak("thomasp85/ggforce")
library(ggforce)
```
```{r}
#| label: status
#| results: "asis"
#| echo: false
# status ----
# polish, dev, draft, complete
status("complete")
```
:::: {.callout-note collapse="false" icon=false}
<br>
```{r}
#| label: full_code_display
#| eval: true
#| echo: false
#| warning: false
#| message: false
#| out-height: '60%'
#| out-width: '60%'
#| fig-align: right
library(palmerpenguins)
library(ggplot2)
library(ggforce)
peng_wide <- palmerpenguins::penguins |>
drop_na() |>
count(island, species, sex) |>
rename(value = n)
para_set_peng <- ggforce::gather_set_data(
data = peng_wide,
x = 1:3)
labs_psets <- labs(
title = "Categories of Palmer Penguins",
y = "Count", fill = "Sex")
ggp2_psets <- ggplot(data = para_set_peng,
mapping = aes(x = x,
id = id,
split = y,
value = value)) +
geom_parallel_sets(aes(fill = sex),
alpha = 0.3,
axis.width = 0.07)
ggp2_psets_axes <- ggp2_psets +
geom_parallel_sets_axes(
axis.width = 0.07)
ggp2_psets_labs <- ggp2_psets_axes +
geom_parallel_sets_labels(
size = 2.0,
color = '#ffffff') +
scale_x_continuous(
breaks = c(1, 2, 3),
labels = c("Island", "Species", "Sex")) +
theme(axis.title.x = element_blank())
ggp2_psets_labs +
labs_psets
```
::: {style="font-size: 1.10em; color: #02577A;"}
**This graph requires:**
:::
::: {style="font-size: 1.05em; color: #282b2d;"}
**`r emo::ji("check")` three categorical variables**
:::
::: {style="font-size: 1.05em; color: #282b2d;"}
**`r emo::ji("check")` a numeric variable of counts**
:::
::::
## Description
Parallel sets (also referred to as Sankey diagrams or Alluvial charts) show the counts of categorical variables connected via a two-sided parallel display (or 'sets'). Parallel sets can also be used to show different states of paired dependent relationships (such as input vs output), or time 1 vs time 2.
The height of the connecting bands *between* the categories on the `x` axis represent the relative counts for each discrete level (displayed on the `y` axis). The levels *within* each variable are represented with color.
We can build parallel set diagrams with the [`ggforce` package](https://ggforce.data-imaginist.com/index.html).
Also check out [alluvial charts.](alluvial_charts.qmd)
## Set up
::: {style="font-size: 1.15em; color: #1e83c8;"}
**PACKAGES:**
:::
Install packages.
::: {style="font-size: 0.85em;"}
```{r}
#| label: pkg_code_parallel_sets
#| code-fold: show
#| eval: false
#| echo: true
#| warning: false
#| message: false
#| results: hide
# pak::pak("thomasp85/ggforce")
install.packages("palmerpenguins")
library(ggforce)
library(palmerpenguins)
library(ggplot2)
```
:::
::: {style="font-size: 1.15em; color: #1e83c8;"}
**DATA:**
:::
::: {.column-margin}
![Artwork by allison horst](www/lter_penguins.png){fig-align="right" width="100%" height="100%"}
:::
We're going to remove the missing values from `palmerpenguins::penguins`, count the categorical variables (`island`, `sex`, `species`), and rename the `n` column (produced by the `count()` function) to `value`.
- `ggforce` has a special [`gather_set_data()`](https://ggforce.data-imaginist.com/reference/gather_set_data.html) function that *changes tidy data into a tidy(er) format*
::: {style="font-size: 0.85em;"}
```{r}
#| label: data_code_parallel_sets
#| code-fold: show
#| eval: true
#| echo: true
peng_wide <- palmerpenguins::penguins |>
drop_na() |>
count(island, species, sex) |>
rename(value = n)
para_set_peng <- ggforce::gather_set_data(
data = peng_wide,
x = 1:3)
dplyr::glimpse(para_set_peng)
```
:::
## Grammar
::: {style="font-size: 1.15em; color: #1e83c8;"}
**CODE:**
:::
- Create labels with `labs()`
- Initialize the graph with `ggplot()` and provide `data`
- Map `x` to `x`, `id` to `id`, `y` to `split`, and `value` to `value`
- In the `geom_parallel_sets()` function, map `sex` to `fill` and manually set the `alpha` (opacity) and the `axis.width`
- In the `geom_parallel_sets_axes()` function, set the `axis.width` to the same value as the `geom_parallel_sets()` above
- For labeling, adjust the `size` manually and set the `color` to something that stands out against the black vertical axes
- Manually label the `x` axis with `scale_x_continuous()`, setting the `breaks` and `labels` to the variable names in the `peng_wide` dataset
- Finally, remove the `x` title with `axis.title.x = element_blank()`
::: {style="font-size: 0.85em;"}
```{r}
#| label: code_graph_parallel_sets
#| code-fold: show
#| eval: false
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
labs_psets <- labs(
title = "Categories of Palmer Penguins",
y = "Count", fill = "Sex")
ggp2_psets <- ggplot(data = para_set_peng,
mapping = aes(x = x,
id = id,
split = y,
value = value)) +
geom_parallel_sets(aes(fill = sex),
alpha = 0.3,
axis.width = 0.07)
ggp2_psets_axes <- ggp2_psets +
geom_parallel_sets_axes(
axis.width = 0.07)
ggp2_psets_labs <- ggp2_psets_axes +
geom_parallel_sets_labels(
size = 2.0,
color = '#ffffff') +
scale_x_continuous(
breaks = c(1, 2, 3),
labels = c("Island", "Species", "Sex")) +
theme(axis.title.x = element_blank())
ggp2_psets_labs +
labs_psets
```
:::
::: {style="font-size: 1.15em; color: #1e83c8;"}
**GRAPH:**
:::
```{r}
#| label: create_graph_parallel_sets
#| eval: true
#| echo: false
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| layout-nrow: 1
#| column: page-inset-right
labs_psets <- labs(
title = "Categories of Palmer Penguins",
y = "Count", fill = "Sex")
ggp2_psets <- ggplot(data = para_set_peng,
mapping = aes(x = x,
id = id,
split = y,
value = value)) +
geom_parallel_sets(aes(fill = sex),
alpha = 0.3,
axis.width = 0.07)
ggp2_psets_axes <- ggp2_psets +
geom_parallel_sets_axes(
axis.width = 0.07)
ggp2_psets_labs <- ggp2_psets_axes +
geom_parallel_sets_labels(
size = 2.0,
color = '#ffffff') +
scale_x_continuous(
breaks = c(1, 2, 3),
labels = c("Island", "Species", "Sex")) +
theme(axis.title.x = element_blank())
ggp2_psets_labs +
labs_psets
```
## More info
If the categories have long names, you can move the location of the labels *outside* the set.
### Labeling sets
If the categories have long names, use the `angle`, `nudge_x`/`nudge_y` and `hjust`/`vjust` in `geom_parallel_sets_labels()` to adjust the size, location, and color of the labels.
- Manually setting the `limits` of the `x` axis in `scale_x_continuous()` will also give more room for the labels.
::: {style="font-size: 0.85em;"}
```{r}
#| label: create_graph_parallel_sets_labs
#| eval: true
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| layout-nrow: 1
#| column: page-inset-right
ggp2_psets_axes +
geom_parallel_sets_labels(
size = 3.2,
colour = '#000000',
angle = 0,
nudge_x = 0.1,
hjust = 0) +
scale_x_continuous(
limits = c(0.9, 3.2),
breaks = c(1, 2, 3),
labels = c("Island", "Species", "Sex")) +
theme(axis.title.x = element_blank()) +
labs_psets
```
:::