【绘图】云雨图比较两组的分布差异

Last updated on September 27, 2025 pm

配置环境

1
2
3
conda create -n raincloud conda-forge::r-tidyverse conda-forge::r-irkernel conda-forge::r-gghalves conda-forge::r-ggbeeswarm -y
# conda install -n raincloud conda-forge::r-ggbeeswarm -y
conda run -n raincloud Rscript -e "IRkernel::installspec(name='raincloud', displayname='r-raincloud')"

使用示例

1
2
3
4
5
6
7
require(ggplot2)
require(gghalves)
require(ggbeeswarm)
require(tidyverse)
require(RColorBrewer)
options(repr.plot.width=6, repr.plot.height=12)
RColorBrewer::display.brewer.all(type = "all")
1
2
data <- iris
data %>% head
1
2
3
4
5
6
7
8
p <- ggplot(data, aes(
x = Species,
y = Sepal.Length,
fill = Species,
color = Species
))
p_color <- RColorBrewer::brewer.pal(3, "Dark2")
p <- p + scale_color_manual(values=rev(p_color)) + scale_fill_manual(values=rev(p_color))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
p1 <- p + geom_half_violin(
nudge = 0.08,
side = 'R', # side:显示哪一侧, "I"代表左侧,"R"代表右侧,默认"I";
trim = T, # trim:小提琴图尾部的数据修整,默认为"T",表示将尾部修整到数据范围;"F"表示不修剪尾部;
alpha = 0.9
) + geom_beeswarm(
aes(x = as.numeric(Species) - 0.08),
side = -1L, # side:显示哪一侧, "-1L"代表左侧,"1L"代表右侧;
size = 2, shape = 20, alpha = 0.8
) + geom_boxplot(
outlier.shape = NA, # 隐藏离群点;
width = 0.1,
alpha = 0.5
) + coord_flip() + theme_bw() + theme(panel.grid=element_blank())
options(repr.plot.width=12, repr.plot.height=9)
p1


【绘图】云雨图比较两组的分布差异
https://hexo.limour.top/distribution-differences-using-raincloud-chart
Author
Limour
Posted on
September 27, 2025
Updated on
September 27, 2025
Licensed under