forked from z3tt/beyond-bar-and-box-plots
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeyondBarAndBoxPlots.Rmd
720 lines (610 loc) · 19.4 KB
/
BeyondBarAndBoxPlots.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
---
title: "Beyond Bar and Box Plots"
description: |
Hands–on code to create and style chart alternatives with ggplot2
author:
- name: "Cédric Scherer"
url: https://cedricscherer.com
date: "`r Sys.Date()`"
output:
distill::distill_article:
highlight: kate
code_folding: false
toc: true
toc_depth: 3
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
dev = "ragg_png", res = 600, retina = 2,
fig.width = 15, fig.height = 8.5)
Sys.setlocale("LC_TIME", "C")
```
## Packages
```{r packages}
library(tidyverse) ## data wrangling + ggplot2
library(colorspace) ## adjust colors
library(rcartocolor) ## Carto palettes
library(ggforce) ## sina plots
library(ggdist) ## halfeye plots
library(ggridges) ## ridgeline plots
library(ggbeeswarm) ## beeswarm plots
library(gghalves) ## off-set jitter
library(systemfonts) ## custom fonts
```
## Import Data
We are using a synthetic data set consisting of four groups with differing sample sizes and diustributions of some values.
```{r data}
url <- "https://raw.githubusercontent.com/z3tt/DataViz-Teaching/master/data/weissgerber-data.csv"
data <- read_csv(url)
```
The data was generated with the code provided in the [flipbook by Nico Riedel, Robert Schulz, and Tracey Weissgerber](https://osf.io/vt2ny/).
## ggplot2 Setup
To use a custom font, one needs to install the .ttf or .otf font files on the local machine.
Here I am using the well-known Roboto typefaces that are available via GoogleFonts:
* [Roboto](https://fonts.google.com/specimen/Roboto?query=roboto)
* [Roboto Mono](https://fonts.google.com/specimen/Roboto?query=roboto+mono)
(In case you can't install the fonts, you might see some warnings that you can ignore. In case it throws an error, remove all arguments related to fonts, i.e. `family` and `base_family` arguments.)
```{r}
## general theme
theme_set(theme_void(base_family = "Roboto"))
theme_update(
axis.text.x = element_text(color = "black", face = "bold", size = 26,
margin = margin(t = 6)),
axis.text.y = element_text(color = "black", size = 22, hjust = 1,
margin = margin(r = 6), family = "Roboto Mono"),
axis.line.x = element_line(color = "black", size = 1),
panel.grid.major.y = element_line(color = "grey90", size = .6),
plot.background = element_rect(fill = "white", color = "white"),
plot.margin = margin(rep(20, 4))
)
## theme for horizontal charts
theme_flip <-
theme(
axis.text.x = element_text(face = "plain", family = "Roboto Mono", size = 22),
axis.text.y = element_text(face = "bold", family = "Roboto", size = 26),
panel.grid.major.x = element_line(color = "grey90", size = .6),
panel.grid.major.y = element_blank(),
legend.position = "top",
legend.text = element_text(family = "Roboto Mono", size = 18),
legend.title = element_text(face = "bold", size = 18, margin = margin(b = 25))
)
## custom colors
my_pal <- rcartocolor::carto_pal(n = 8, name = "Bold")[c(1, 3, 7, 2)]
```
## Summary Plots
### Barplot
```{r barplot-mean}
ggplot(data, aes(x = group, y = value, fill = group)) +
geom_bar(stat = "summary", width = .8) +
scale_y_continuous(expand = c(0, 0), breaks = 0:4) +
scale_fill_manual(values = my_pal, guide = "none")
```
> Most data are not as clean as bar plots make them seem, and since bar plots reveal very little about the distribution of the data, this kind of visualization can be misleading. A further issue is that of the bar itself, which implies that the base of the y-axis is meaningful, which is not necessarily the case. ~ #barbarplot intiative
```{r barplot-count}
ggplot(data, aes(x = group, fill = group)) +
geom_bar(width = .8) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = my_pal, guide = "none")
```
### Dynamite Plot
> Dynamite plots are used to compare measurements from two or more groups: cases and controls, for example. In a two group comparison, the plots are graphical representations of a grand total of 4 numbers, regardless of the sample size. The four numbers are the average and the standard error (or the standard deviation, it’s not always clear) for each group. ~ Rafael Irizarry, *"Dynamite Plots Must Die"*
```{r dynamite-plot}
ggplot(data, aes(x = group, y = value, color = group, fill = group)) +
stat_summary(
geom = "errorbar",
fun.max = function(x) mean(x) + sd(x),
fun.min = function(x) mean(x) - sd(x),
width = .3, size = 1.2
) +
geom_bar(stat = "summary", width = .8, size = .8) +
scale_y_continuous(expand = c(0, 0), breaks = 1:9) +
scale_fill_manual(values = my_pal, guide = "none") +
scale_color_manual(values = my_pal, guide = "none")
```
### Boxplot
```{r object}
g <- ggplot(data, aes(x = group, y = value, color = group, fill = group)) +
scale_y_continuous(breaks = 1:9) +
scale_color_manual(values = my_pal, guide = "none") +
scale_fill_manual(values = my_pal, guide = "none")
```
```{r boxplot-alpha}
g +
geom_boxplot(alpha = .5, size = 1.5, outlier.size = 5)
```
```{r boxplot-afterscale}
g +
geom_boxplot(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.5, outlier.size = 5
)
```
> While box plots are highly effective and widely used in data analytics, they are limited in the fact that they only show specific statistical points, such as the median average or outliers, rather than the distribution of a data set as a whole. ~ LondonSoda.com
### Violin Plot
> Violin plots focus on illustrating the distribution of the entire data set and can generate different insights, that are hidden in the structure of box plots. ~ LondonSoda.com
```{r violin-area}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2
)
```
```{r violin-bandwidth}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2
)
```
```{r violin-bandwidth-8}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .8
)
```
```{r violin-bandwidth-05}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .05
)
```
```{r violin-count}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2, scale = "count"
)
```
```{r violin-halfeye-raw}
g +
ggdist::stat_halfeye(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7)))
)
```
```{r violin-halfeye}
g +
ggdist::stat_halfeye(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
adjust = .2, position = position_nudge(x = -.3)
)
```
```{r violin-halfeye-adj}
g +
ggdist::stat_halfeye(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
.width = 1, point_size = 5, adjust = .2, position = position_nudge(x = -.3)
)
```
### Ridgeline Plot
> Ridgeline plots are partially overlapping line plots that create the impression of a mountain range. They can be quite useful for visualizing changes in distributions over time or space. ~ {ggridges} reference
```{r ridgeline}
g_ridges <-
ggplot(data, aes(value, fct_rev(group), color = group, fill = group)) +
coord_cartesian(clip = "off") +
scale_y_discrete(expand = c(.07, .07)) +
scale_color_manual(values = my_pal, guide = "none") +
scale_fill_manual(values = my_pal, guide = "none") +
theme_flip
g_ridges +
ggridges::geom_density_ridges(
alpha = .7, size = 1.5
)
```
```{r ridgeline-cut}
g_ridges +
ggridges::geom_density_ridges(
alpha = .8, size = 1.5,
rel_min_height = 0.01
)
```
```{r ridgeline-gradient, include=FALSE}
g_ridges +
ggridges::geom_density_ridges_gradient(
aes(fill = stat(x)), color = "black",
size = 1.5, rel_min_height = 0.01
) +
scale_fill_viridis_c(
option = "mako", direction = -1, guide = "none", end = .9
)
```
```{r ridgeline-median}
g_ridges +
ggridges::stat_density_ridges(
quantile_lines = TRUE, quantiles = 2,
color = "black", alpha = .8, size = 1.5
)
```
```{r ridgeline-quartiles}
q_pal <- colorspace::lighten(rcartocolor::carto_pal(n = 4, name = "Geyser")[c(2, 1, 4, 3)], .6)
#q_pal <- colorspace::adjust_transparency(rcartocolor::carto_pal(n = 4, name = "Tropic")[c(2, 1, 4, 3)], alpha = .7)
g_ridges +
ggridges::stat_density_ridges(
aes(fill = factor(stat(quantile))),
geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = 4,
color = "black", size = 1
) +
scale_fill_manual(values = q_pal, name = "Quartile:") +
guides(fill = guide_legend(override.aes = list(color = "transparent")))
```
```{r ridgeline-quartiles-bw}
g_ridges +
ggridges::stat_density_ridges(
aes(fill = factor(stat(quantile))),
geom = "density_ridges_gradient", calc_ecdf = TRUE,
quantiles = 4,
color = "black", size = 1,
bandwidth = .1
) +
scale_fill_manual(values = q_pal, name = "Quartile:") +
guides(fill = guide_legend(override.aes = list(color = "transparent")))
```
(Note that now it's clearly visible that the quantile lines are off, at leastc for the group D with the low sample size.)
```{r ridgeline-tails}
g_ridges +
ggridges::stat_density_ridges(
aes(fill = factor(stat(quantile))),
geom = "density_ridges_gradient", calc_ecdf = TRUE,
quantiles = c(0.025, 0.975),
color = "black", size = 1.5
) +
scale_fill_manual(
name = "Probability:", values = c("#994c00", "grey70", "#003366"),
labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]")
) +
guides(fill = guide_legend(override.aes = list(color = "transparent")))
```
### Interval strips
```{r interval-strips-raw}
g_interval <-
ggplot(data, aes(group, value)) +
scale_color_viridis_d(
option = "mako", name = "Level:", direction = -1,
begin = .15, end = .9
) +
guides(
color = guide_legend(reverse = TRUE, title.position = "top")
) +
theme(
legend.position = c(.75, .95), legend.direction = "horizontal",
legend.text = element_text(family = "Roboto Mono", size = 18),
legend.title = element_text(face = "bold", size = 22, hjust = .5)
)
g_interval +
ggdist::stat_interval(size = 12)
```
```{r interval-strips}
g_interval +
ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12)
```
```{r interval-strips-percent}
g_interval +
ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12) +
scale_color_viridis_d(
option = "mako", name = "Level:", direction = -1,
begin = .15, end = .9,
labels = function(x) paste0(as.numeric(x)*100, "%")
)
```
```{r interval-strips-median}
g_interval +
ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12) +
stat_summary(
geom = "point", fun = median,
color = "white", size = 6, shape = 1, stroke = 1.6
) +
scale_color_viridis_d(
option = "mako", name = "Level:", direction = -1,
begin = .15, end = .9,
labels = function(x) paste0(as.numeric(x)*100, "%")
)
```
```{r interval-strips-density}
g_interval +
ggdist::stat_interval(
.width = c(.25, .5, .95, 1),
size = 7
) +
ggdist::stat_halfeye(
adjust = .33, ## bandwidth
width = .7, fill = "grey85",
interval_colour = NA, point_colour = "black",
shape = 23, stroke = 1.5, point_size = 5, point_fill = "white",
position = position_nudge(x = .03)
) +
scale_color_viridis_d(
option = "mako", name = "Level:", direction = -1,
begin = .15, end = .9,
labels = function(x) paste0(as.numeric(x)*100, "%")
)
```
### Gradient Interval
```{r gradient-interval}
g +
ggdist::stat_gradientinterval(
width = .3, color = "black"
)
```
## Raw Data Charts
### Scatter Plot (Strip Chart)
```{r dotstrips}
g + geom_point(size = 10, alpha = .33)
```
### Barcode Plots (Strip Chart)
```{r barcode}
g + geom_point(shape = 95, size = 50, alpha = .33)
```
### Jitter Strip Chart
```{r jitter-a}
g + geom_jitter(size = 7, alpha = .5)
```
```{r jitter-b}
g +
geom_point(
position = position_jitter(width = .2, seed = 0),
size = 7, alpha = .5
)
```
```{r jitter-outline}
g +
geom_point(
position = position_jitter(width = .2, seed = 0),
size = 7, alpha = .5
) +
geom_point(
position = position_jitter(width = .2, seed = 0),
size = 7, stroke = .9, shape = 1, color = "black"
)
```
### Sina Plot
> The sina plot is a data visualization chart suitable for plotting any single variable in a multiclass dataset. It is an enhanced jitter strip chart, where the width of the jitter is controlled by the density distribution of the data within each class. ~ {ggforce} reference
```{r sina}
g +
ggforce::geom_sina(
maxwidth = .6, scale = "count", seed = 1,
size = 7, alpha = .5
) +
ggforce::geom_sina(
maxwidth = .6, scale = "count", seed = 1,
size = 7, shape = 1, color = "black", stroke = .8
)
```
### Dot Plot
> A dot plot or dot chart is a statistical chart consisting of data points plotted on a fairly simple scale, typically using filled in circles. There are two common versions of the dot chart. The first is described by Leland Wilkinson as a graph that has been used in hand-drawn (pre-computer era) graphs to depict distributions. The other version is described by William Cleveland as an alternative to the bar chart, in which dots are used to depict the quantitative values (e.g. counts) associated with categorical variables. ~ datavizproject.com
```{r dotplot}
g +
ggdist::stat_dots(position = position_nudge(x = -.25))
```
```{r dotplot-symmetrical}
g +
ggdist::stat_dots(side = "both")
```
```{r dotplot-off-axis}
g +
ggdist::stat_dots(layout = "weave", position = position_nudge(x = -.25))
```
### Beeswarm Plot
```{r beeswarm-ggdist}
g +
ggdist::stat_dots(layout = "swarm", side = "both")
```
```{r beeswarm-ggbeeswarm}
g +
ggbeeswarm::geom_beeswarm(size = 8, cex = 3)
```
```{r beeswarm-ggbeeswarm-quasi}
g +
ggbeeswarm::geom_quasirandom(size = 8, width = .33, alpha = .7) +
ggbeeswarm::geom_quasirandom(size = 8, width = .33, shape = 1, color = "black", stroke = .8)
```
## Hybrid Charts
### Beeswarm with Median Indicator
```{r beeswarm-median}
g +
ggbeeswarm::geom_quasirandom(
size = 8, width = .33, alpha = .3
) +
stat_summary(
fun = median, geom = "point",
shape = 95, size = 50
) +
ggbeeswarm::geom_quasirandom(
size = 8, width = .33, shape = 1, color = "black", stroke = .8
)
```
### Box Plot x Jitter Strips
```{r boxplot-jitter}
g +
geom_boxplot(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
size = 1.5, outlier.shape = NA
) +
geom_jitter(width = .1, size = 7, alpha = .5)
```
```{r boxplot-jitter-outlines}
g +
geom_boxplot(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
size = 1.5, outlier.shape = NA
) +
geom_point(
position = position_jitter(width = .1, seed = 0),
size = 7, alpha = .5
) +
geom_point(
position = position_jitter(width = .1, seed = 0),
size = 7, stroke = .9, shape = 1, color = "white"
)
```
### Box Plot x Violin Plot
```{r violin-boxplot-raw}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2
) +
geom_boxplot(
fill = "white", size = 1.2, width = .2, outlier.size = 5
)
```
```{r violin-boxplot}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2
) +
geom_boxplot(
fill = "white", size = 1.2, width = .2,
outlier.shape = NA, coef = 0
)
```
```{r violin-boxplot-styled}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2, color = NA
) +
geom_boxplot(
width = .1, size = 1.2, outlier.shape = NA
) +
stat_summary(
geom = "point",
fun = median,
color = "white",
size = 5
)
```
### Box Plot x Violin Plot x Jitter Strips
```{r violin-boxplot-jitter}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2
) +
geom_boxplot(
fill = "white", size = 1.2, width = .2,
outlier.shape = NA, coef = 0
) +
geom_point(
position = position_jitter(width = .03, seed = 0),
size = 5, alpha = .5
) +
geom_point(
position = position_jitter(width = .03, seed = 0),
size = 5, stroke = .7, shape = 1, color = "black"
)
```
### Box Plot x Violin Plot x Beeswarm Plot
```{r violin-boxplot-beeswarm, include=FALSE}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
size = 1.2, bw = .2
) +
geom_boxplot(
fill = "white", size = 1.2, width = .2,
outlier.shape = NA, coef = 0
) +
ggdist::stat_dots(
layout = "swarm", side = "both",
stackratio = .25, dotsize = .1,
alpha = .5
) +
ggdist::stat_dots(
layout = "swarm", side = "both",
stackratio = .25, dotsize = .1,
shape = 1, color = "black", stroke = .8
)
```
### Violin Plot x Sina Plots
```{r violin-count-sina}
g +
geom_violin(
aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
size = 1.2, bw = .2, width = .6, scale = "count"
) +
stat_summary(
geom = "point",
fun = median,
shape = 23, size = 6, color = "black", stroke = 1.5
) +
ggforce::geom_sina(
maxwidth = .5, scale = "count",
size = 3, alpha = .5, seed = 0
) +
ggforce::geom_sina(
maxwidth = .5, scale = "count",
size = 3, alpha = .5, seed = 0,
shape = 1, color = "black", stroke = .8
)
```
## Raincloud Plots
> A data visualization approach which [provides] maximal statistical information while preserving the desired ‘inference at a glance’ nature of barplots and other similar visualization devices. These “raincloud plots” can visualize raw data, probability density, and key summary statistics such as median, mean, and relevant confidence intervals in an appealing and flexible format with minimal redundancy. ~ Allen et al. (2021) Wellcome Open Res 4:63
```{r raincloud}
g +
geom_boxplot(
width = .2, fill = "white",
size = 1.5, outlier.shape = NA
) +
ggdist::stat_halfeye(
adjust = .33, ## bandwidth
width = .67,
color = NA, ## remove slab interval
position = position_nudge(x = .15)
) +
gghalves::geom_half_point(
side = "l",
range_scale = .3,
alpha = .5, size = 3
)
```
```{r raincloud-flip, fig.height = 10}
ggplot(data, aes(x = forcats::fct_rev(group), y = value,
color = group, fill = group)) +
geom_boxplot(
width = .2, fill = "white",
size = 1.5, outlier.shape = NA
) +
ggdist::stat_halfeye(
adjust = .33,
width = .67,
color = NA,
position = position_nudge(x = .15)
) +
gghalves::geom_half_point(
side = "l",
range_scale = .3,
alpha = .5, size = 3
) +
coord_flip() +
scale_x_discrete(expand = c(.07, .07)) +
scale_y_continuous(breaks = 1:9) +
scale_color_manual(values = my_pal, guide = "none") +
scale_fill_manual(values = my_pal, guide = "none") +
theme_flip
```
```{r raincloud-barcode}
g +
geom_boxplot(
width = .2, fill = "white",
size = 1.5, outlier.shape = NA
) +
ggdist::stat_halfeye(
adjust = .33,
width = .55,
color = NA,
position = position_nudge(x = .14)
) +
geom_point(
position = position_nudge(x = -.22),
shape = 95, size = 24, alpha = .25
)
```
***
<details><summary>Session Info</summary>
```{r sessionInfo}
Sys.time()
sessionInfo()
```
</details>