forked from clauswilke/dataviz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectory_of_visualizations.Rmd
350 lines (272 loc) · 13.6 KB
/
directory_of_visualizations.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
```{r echo = FALSE, message = FALSE}
# run setup script
source("_common.R")
library(dplyr)
library(ggforce)
library(ggridges)
```
# Directory of visualizations
```{r}
## general setup code
# theme
theme_plot_icon <- function(bg_color = "#F5F8EA", line_color = "#243400") {
line_size <- .8
font_size <- 10
theme_half_open() %+replace% theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.line.x = element_blank(),
#axis.line.y = element_blank(),
#axis.ticks = element_blank(),
axis.line.x = element_line(size = line_size, color = line_color),
axis.line.y = element_line(size = line_size, color = line_color),
axis.ticks = element_line(size = line_size, color = line_color),
axis.ticks.length = grid::unit(4, "pt"),
legend.position = "none",
plot.margin = margin(2, font_size, font_size, font_size),
plot.title = element_text(hjust = 0.5, face = "bold", color = line_color,
size = font_size,
margin = margin(1, 0, 2, 0)),
plot.background = element_rect(fill = bg_color)
)
}
# data sets
set.seed(5142)
n <- 15
x <- rnorm(n)
y <- .4*x + .6*rnorm(n)
df_scatter_xy <- data.frame(x, y)
df_one_dist <- data.frame(x = c(rnorm(1000, 1., 1.6), rnorm(350, 4, .4)))
df_one_normal <- data.frame(x = rnorm(20))
df_fractions <- data.frame(y = c(.3, .39, .48, .6, .25, .13, .22, .24, .45, .48, .3, .16),
x = factor(rep(1:4, 3)),
type = rep(c("A", "B", "C"), each = 4))
n <- 70
df_multi_dist <- data.frame(y = c(rnorm(n, 1, .8), rnorm(n, 2, .7), rnorm(n, 0, .5)),
type = rep(c("A", "B", "C"), each = n),
number = rep(c(2, 1, 3), each = n))
df_props = data.frame(value = c(60, 30, 10),
group = c("A", "B", "C"))
# palettes
npal <- 5
# earth-brown
pal_earth_brown <- sequential_hcl(n = npal, h1 = 71, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# brown-green
pal_brown_green <- sequential_hcl(n = npal, h1 = 86, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# green-brown
pal_green_brown <- sequential_hcl(n = npal, h1 = -265, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# burgundy-red
pal_red_brown <- sequential_hcl(n = npal, h1 = 28, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# brown-red
pal_brown_red <- sequential_hcl(n = npal, h1 = 41, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# ocean-blue
pal_ocean_blue <- sequential_hcl(n = npal, h1 = 241, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
# steel-blue
pal_steel_blue <- sequential_hcl(n = npal, h1 = 257, c1 = 80, c2 = 10, l1 = 18, l2 = 97, p1 = 1.5)
```
## Proportions
```{r proportions, fig.width = 8, fig.asp = 1/4}
palette <- pal_brown_green
p1_main <- ggplot(df_props, aes(x = 1, y = value, fill = group)) +
geom_col(position = "stack", color = palette[1]) +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL, name = "") +
scale_x_continuous(breaks = NULL, name = "") +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Pie chart") +
theme_plot_icon(palette[npal], palette[1]) +
theme(axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank())
# make sure plot background is fully filled, as in the other plots
p1 <- ggdraw(p1_main) +
theme(plot.background = element_rect(fill = palette[npal],
color = "transparent"))
p2 <- ggplot(df_props, aes(x = factor(1), y = value, fill = group)) +
geom_col(position = position_stack(reverse = TRUE), width = .45, color = palette[1]) +
scale_y_continuous(limits = c(0, 110), expand = c(0, 0)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked bars") +
theme_plot_icon(palette[npal], palette[1])
p3 <- ggplot(df_props, aes(x = factor(1), y = value, fill = group)) +
geom_col(position = position_stack(reverse = TRUE), width = .45, color = palette[1]) +
#scale_y_continuous(limits = c(0, 110), expand = c(0, 0), position = "right") +
scale_y_continuous(limits = c(0, 110), expand = c(0, 0)) +
coord_flip() +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked bars") +
theme_plot_icon(palette[npal], palette[1])
p4 <- ggplot(df_props, aes(x = group, y = value, fill = group)) +
geom_col(position="identity", color = palette[1],
width = .8) +
scale_y_continuous(limits = c(0, 66), expand = c(0, 0)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Side-by-side bars") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p4, p2, p3, ncol = 4, scale = .9)
```
Proportions can be visualized as pie charts, side-by-side bars, or stacked bars, the latter arranged either vertically or horizontally (Chapter \@ref(visualizing-proportions)). Pie charts emphasize that the individual parts add up to a whole and highlight simple fractions. However, the individual pieces are more easily compared in side-by-side bars. Stacked bars look awkward for a single set of proportions, but can be useful when comparing multiple sets of proportions (see below).
```{r proportions-comp, fig.width = 8, fig.asp = 1/4}
palette <- pal_earth_brown
p5 <- ggplot(filter(df_fractions, x!=4), aes(x, y,
fill=factor(type, levels = c("A", "C", "B")))) +
geom_col(position="dodge", color = palette[1],
width = .7) +
scale_y_continuous(expand = c(0, 0),
limits = c(0, .58)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Side-by-side bars") +
theme_plot_icon(palette[npal], palette[1])
p6 <- ggplot(df_fractions, aes(x, y, fill=type)) +
geom_col(position="stack", color = palette[1]) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked bars") +
theme_plot_icon(palette[npal], palette[1])
p7 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_density(alpha = 0.7, color = palette[1], position = "fill") +
scale_fill_manual(values = palette[2:4]) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
labs(title = "Stacked densities") +
theme_plot_icon(palette[npal], palette[1])
p8_a <- ggplot(filter(df_fractions, x==1), aes(x = 1, y = y, fill = type)) +
geom_col(position = "stack", color = palette[1]) +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL, name = "") +
scale_x_continuous(breaks = NULL, name = "") +
scale_fill_manual(values = palette[2:4]) +
theme_plot_icon(palette[npal], palette[1]) +
theme(axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.background = element_blank())
p8_b <- ggplot(filter(df_fractions, x==2), aes(x = 1, y = y, fill = type)) +
geom_col(position = "stack", color = palette[1]) +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL, name = "") +
scale_x_continuous(breaks = NULL, name = "") +
scale_fill_manual(values = palette[2:4]) +
theme_plot_icon(palette[npal], palette[1]) +
theme(axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.background = element_blank())
p8_c <- ggplot(filter(df_fractions, x==3), aes(x = 1, y = y, fill = type)) +
geom_col(position = "stack", color = palette[1]) +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL, name = "") +
scale_x_continuous(breaks = NULL, name = "") +
scale_fill_manual(values = palette[2:4]) +
theme_plot_icon(palette[npal], palette[1]) +
theme(axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
plot.background = element_blank())
# combine
p8 <- ggdraw(plot_grid(p8_a, p8_b, p8_c, ncol = 3, scale = 1.4)) +
labs(title = "Multiple pie charts") +
theme(plot.title = element_text(hjust = 0.5, face = "bold", color = palette[1],
size = 10, margin = margin(1, 0, 2, 0)),
plot.background = element_rect(fill = palette[npal], color = "transparent"))
plot_grid(p8, p5, p6, p7, ncol = 4, scale = .9)
```
When visualizing multiple sets of proportions or changes in proportions across conditions, pie charts tend to be space-inefficient and often obscure relationships. Side-by-side bars work well as long as the number of conditions compared is moderate, and stacked bars can work for large numbers of conditions. Stacked densities (Chapter \@ref(visualizing-proportions)) are appropriate when the proportions change along a continuous variable.
## Individual distributions
```{r single-distributions, fig.width = 8, fig.asp = 1/4}
palette <- pal_brown_red
p1 <- ggplot(df_one_dist, aes(x)) +
geom_histogram(fill = palette[4], color = palette[2], bins = 18) +
scale_x_continuous(limits = c(-4.8, 6.8), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 290),
expand = c(0, 0)) +
labs(title = "Histogram") +
theme_plot_icon(palette[npal], palette[1])
p2 <- ggplot(df_one_dist, aes(x)) +
geom_density(fill = palette[4], color = palette[2], bw = .35) +
scale_x_continuous(limits = c(-4.8, 6.8), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, .29), expand = c(0, 0)) +
labs(title = "Density plot") +
theme_plot_icon(palette[npal], palette[1])
p3 <- ggplot(df_one_normal, aes(x)) +
stat_ecdf(color = palette[2], size = .7) +
scale_x_continuous(expand = c(0.05, 0)) +
scale_y_continuous(limits = c(0, 1.1), expand = c(0, 0)) +
labs(title = "Cumulative density") +
theme_plot_icon(palette[npal], palette[1])
p4 <- ggplot(df_one_normal, aes(sample = x)) +
geom_abline(intercept = 0, slope = 1, color = palette[4]) +
geom_qq(color = palette[2]) +
labs(title = "q-q plot") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p2, p3, p4, ncol = 4, scale = .9)
```
Histograms and density plots (Chapter \@ref(histograms-density-plots)) provide the most intuitive visualizations of a distribution, but both require arbitrary parameter choices and can be misleading. Cumulative densities and q-q plots (Chapter \@ref(ecdf-qq)) always represent the data faithfully but can be more difficult to interpret.
## Multiple distributions
```{r multiple-distributions, fig.width = 8, fig.asp = 1/2}
palette <- pal_ocean_blue
p1 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_boxplot(color = palette[1], fill = palette[4]) +
labs(title = "Boxplots") +
theme_plot_icon(palette[npal], palette[1])
p2 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_violin(color = palette[1], fill = palette[4]) +
labs(title = "Violin plots") +
theme_plot_icon(palette[npal], palette[1])
p3 <- ggplot(df_multi_dist, aes(x = type, y = y)) + geom_jitter(color = palette[1], width = 0.3, size = .8) +
labs(title = "Jittered points") +
theme_plot_icon(palette[npal], palette[1])
p4 <- ggplot(df_multi_dist, aes(x = type, y = y)) +
geom_sina(color = palette[1], size = 0.8) +
labs(title = "Sina plot") +
theme_plot_icon(palette[npal], palette[1])
p5 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_histogram(color = palette[1], bins = 12) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Stacked histograms") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 49), expand = c(0, 0)) +
theme_plot_icon(palette[npal], palette[1])
p6 <- ggplot(df_multi_dist, aes(x = y, fill = factor(type, levels = c("C", "A", "B")))) +
geom_density(alpha = 0.7, color = palette[1]) +
scale_fill_manual(values = palette[2:4]) +
labs(title = "Overlapping densities") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, .95), expand = c(0, 0)) +
theme_plot_icon(palette[npal], palette[1])
p7 <- ggplot(df_multi_dist, aes(x = y, y = number, group = number)) +
geom_density_ridges(alpha = 0.7, color = palette[1], fill = palette[3], scale = 2.0) +
labs(title = "Ridgeline plot") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(1, 6.5), expand = c(0, 0)) +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, p2, p3, p4,
p5, p6, p7, ncol = 4, scale = .9)
```
Boxplots, violin plots, jittered points, and sina plots are useful when we want to visualize many distributions at once and/or if we are primarily interested in overall shifts among the distributions (Chapter \@ref(boxplots-violins)). Stacked histograms and overlapping densities allow a more in-depth comparison of a smaller number of distributions, though stacked histograms can be difficult to interpret and are best avoided (Chapter \@ref(histograms-density-plots)). Ridgeline plots can be a useful alternative to violin plots and are often useful when visualizing very large numbers of distributions or changes in distributions over time (Chapter \@ref(boxplots-violins)).
```{r fig.width = 8, fig.asp = 1/4, include = FALSE}
## Other
palette <- pal_steel_blue
p1 <- ggplot(df_scatter_xy, aes(x, y)) +
geom_point(color = palette[3], size = 2.4) +
scale_x_continuous(expand = c(.2, 0)) +
scale_y_continuous(expand = c(.2, 0)) +
labs(title = "Scatterplot") +
theme_plot_icon(palette[npal], palette[1])
plot_grid(p1, ncol = 4, scale = .9)
## plots to add:
# x-y relationships:
# - 2d bins
# - hexbins
# - smooth line with error region
# - multiple lines
# quantities
# - horizontal bars, ordered
# proportions
# - mosaic plot/stacked bars flipped to horizontal
# - pie chart
```