使用ggplot2绘制云雨图
raincloud
图用是一种用来展示数据分布的图,因其形状类似云雨而得名。
# CREDIT ----
# Cedric Scherer
# https://www.cedricscherer.com/2021/06/06/visualizing-distributions-with-raincloud-plots-with-ggplot2/
# LIBRARIES ----
library(ggdist)
library(tidyquant)
library(tidyverse)
# DATA -----
mpg
# RAINCLOUD PLOTS ----
# - Very powerful for visualizing modality of distributions
mpg %>%
filter(cyl %in% c(4,6,8)) %>%
ggplot(aes(x = factor(cyl), y = hwy, fill = factor(cyl))) +
# add half-violin from {ggdist} package
ggdist::stat_halfeye(
## custom bandwidth
adjust = 0.5,
## move geom to the right
justification = -.2,
## remove slab interval
.width = 0,
point_colour = NA
) +
geom_boxplot(
width = .12,
## remove outliers
outlier.color = NA,
alpha = 0.5
) +
# Add dot plots from {ggdist} package
ggdist::stat_dots(
## orientation to the left
side = "left",
## move geom to the left
justification = 1.1,
## adjust grouping (binning) of observations
binwidth = .25
) +
# Adjust theme
scale_fill_tq() +
theme_tq() +
labs(
title = "Raincloud Plot",
subtitle = "Showing the Bi-Modal Distribution of 6 Cylinder Vehicles",
x = "Engine Size (No. of Cylinders)",
y = "Highway Fuel Economy (MPG)",
fill = "Cylinders"
) +
coord_flip()
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
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
编辑 (opens new window)
上次更新: 2021/07/23, 09:15:44