收集了幾張漂亮的組間比較圖们妥,調(diào)整了一些細(xì)節(jié)猜扮,分享給大家。使用的數(shù)據(jù)時(shí)R語言內(nèi)置數(shù)據(jù)iris监婶,以下代碼都可以直接運(yùn)行旅赢。
1.ggstatplot
難點(diǎn)是這個(gè)R包安裝時(shí)通常會遇到一些依賴包安裝不成功的問題,需要多折騰幾下压储,安好了使用起來就非常輕松咯
library(ggstatsplot)
ggbetweenstats(iris,x = "Species",y = "Sepal.Length")
2.經(jīng)典箱線圖疊加點(diǎn)圖
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又比較呆板,有個(gè)折中的圖:蜜蜂圖
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.微笑版密蜂圖加分位數(shù)線
有些糾結(jié)到底應(yīng)該是疊加分位數(shù)線集惋,還是疊加誤差棒孕似,發(fā)現(xiàn)其實(shí)兩個(gè)都說的過去,干脆都畫一下咯刮刑。
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
## <fct> <dbl> <dbl>
## 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