组间比较不只有箱线图,还有这些

收集了几张漂亮的组间比较图,调整了一些细节,分享给大家。使用的数据时R语言内置数据iris,以下代码都可以直接运行。

1.ggstatplot

难点是这个R包安装时通常会遇到一些依赖包安装不成功的问题,需要多折腾几下,安好了使用起来就非常轻松咯

library(ggstatsplot)
ggbetweenstats(iris,x = "Species",y = "Sepal.Length")

2.经典箱线图叠加点图

library(ggplot2)
library(ggpubr)
library(paletteer)
ggplot(iris,aes(x = Species,y=Sepal.Length))+
  geom_boxplot(aes(fill = Species))+
  geom_jitter(shape = 21,size = 2,color = "black",aes(fill = Species),stroke = 1.5)+
  scale_fill_paletteer_d("basetheme::minimal")+
  stat_compare_means(method = "wilcox.test",comparisons = list(c("setosa","virginica"),c("setosa","versicolor"),c("virginica","versicolor")))+
  theme_bw()+
  theme(legend.position = c("top"),panel.grid = element_blank())
ggplot(iris,aes(x = Species,y=Sepal.Length))+
  geom_boxplot(aes(fill = Species))+
  geom_dotplot(binaxis = "y",binwidth = 0.12,stackdir = "center",stroke = 1.5,aes(fill = Species))+
  scale_fill_paletteer_d("basetheme::minimal")+
  stat_compare_means(method = "wilcox.test",comparisons = list(c("setosa","virginica"),c("setosa","versicolor"),c("virginica","versicolor")))+
  theme_bw()+
  theme(legend.position = c("top"),panel.grid = element_blank())

jitter太跳脱了,dotplot又比较呆板,有个折中的图:蜜蜂图

3.箱线图叠加蜜蜂图

library(ggbeeswarm)
ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species))+
  geom_boxplot()+
  geom_beeswarm(size = 3,cex = 3,shape = 21,stroke = 1.5)+
  theme_bw()+
  theme(legend.position = c("top"),panel.grid = element_blank())+
  stat_compare_means(method = "wilcox.test",comparisons = list(c("setosa","virginica"),c("setosa","versicolor"),c("virginica","versicolor")))+
  scale_fill_paletteer_d("basetheme::minimal")

4.微笑版密蜂图加分位数线

有些纠结到底应该是叠加分位数线,还是叠加误差棒,发现其实两个都说的过去,干脆都画一下咯。

ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species))+
  geom_quasirandom(method = "smiley",size = 3,width = 0.25,shape = 21,stroke = 1.5)+
  theme_bw()+
  theme(legend.position = c("top"),panel.grid = element_blank())+
  stat_compare_means(method = "wilcox.test",comparisons = list(c("setosa","virginica"),c("setosa","versicolor"),c("virginica","versicolor")))+
  stat_summary(fun = median, fun.min = median, fun.max = median, 
               geom = 'crossbar', width = 0.3, size = 0.4,color = 'black') +
  stat_summary(fun.data = function(x) median_hilow(x, 0.5), 
               geom = 'errorbar', width = 0.25, size = 1,color = 'black')+
  scale_fill_paletteer_d("basetheme::minimal")

5.微笑版蜜蜂图叠加误差棒

library(dplyr)
df2 <- group_by(iris,Species)%>%summarise(sd = sd(Sepal.Length),
                                          Sepal.Length=mean(Sepal.Length))
head(df2)
## # A tibble: 3 x 3
##   Species       sd Sepal.Length
##                 
## 1 setosa     0.352         5.01
## 2 versicolor 0.516         5.94
## 3 virginica  0.636         6.59
ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species))+
  geom_quasirandom(method = "smiley",size = 3,width = 0.25,shape = 21,stroke = 1.5)+
  theme_bw()+
  theme(legend.position = c("top"),panel.grid = element_blank())+
  stat_compare_means(method = "wilcox.test",comparisons = list(c("setosa","virginica"),c("setosa","versicolor"),c("virginica","versicolor")))+
  stat_summary(fun = median, fun.min = median, fun.max = median, 
               geom = 'crossbar', width = 0.3, size = 0.4,color = 'black')+
  geom_errorbar(dat = df2,aes(ymin=Sepal.Length-sd, ymax=Sepal.Length+sd), width=.2)+
  scale_fill_paletteer_d("basetheme::minimal")

参考代码:http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization
https://paulvanderlaken.com/2019/01/25/visualization-innovation-waffleplots-and-swarmplots-aka-beeplots/
https://mp.weixin.qq.com/s/8LwTRKTlOR0CsQDUc15sBA

你可能感兴趣的:(组间比较不只有箱线图,还有这些)