作者:一只想飛的喵
審稿:童蒙
編輯:angelica
箱線圖是統(tǒng)計(jì)學(xué)中較常見(jiàn)的圖形之一。這篇文章將講述如何簡(jiǎn)單比較兩組或多組的平均值,且添加顯著性標(biāo)記。
通常情況根據(jù)顯著性p值的數(shù)值大小,分為四類:
(1)0.01≤p<0.05,*
(2)0.001≤p<0.01,**
(3)0.0001≤p<0.001,***
(4)p<0.0001, ****
接下來(lái)會(huì)講述三種添加顯著性標(biāo)記的方法。
方法1-手動(dòng)添加
1:創(chuàng)建數(shù)據(jù),繪制箱線圖。
library(ggplot2)
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_boxplot()
2:手動(dòng)添加顯著性標(biāo)記。
library(ggplot2)
### 設(shè)置橫線的四個(gè)點(diǎn)的位置
df2 <- data.frame(a = c(2,2,3,3), b = c(8,8.1,8.1,8))
ggplot(iris, aes(x=Species, y=Sepal.Length))+
geom_boxplot()+
### 按照設(shè)定的位置繪制線
geom_line(data = df2, aes(x = a, y = b)) +
### 添加顯著性標(biāo)記信息
annotate("text", x = 2.5, y = 8.2, label = "***", size = 8)
該方法的缺點(diǎn)是:在手動(dòng)添加顯著性時(shí),需要確認(rèn)顯著性的P值的大小。
方法2-使用ggsignif
1:下載并安裝ggsignif包。
install.packages("ggsignif")
2:創(chuàng)建數(shù)據(jù),繪制箱線圖。
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
3:利用ggsignif R包添加顯著性標(biāo)記。
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
### 添加兩兩比較的列的信息
geom_signif(comparisons = list(c("versicolor", "virginica")),
### P值<0.05,則顯示
map_signif_level=TRUE)
方法3-使用ggpubr
1:下載并安裝ggpubr包。
install.packages("ggpubr")
2:創(chuàng)建數(shù)據(jù),繪制箱線圖。
library(ggpubr)
### 添加兩兩比較的列表
my_comparisons <- list(c("setosa", "versicolor"), c("setosa", "virginica"), c("versicolor", "virginica"))
ggboxplot(iris, x="Species", y="Sepal.Length",color = "Species",palette = "jco", add = "jitter")
3:利用ggpubr R包添加顯著性標(biāo)記。
library(ggpubr)
my_comparisons <- list(c("setosa", "versicolor"), c("setosa", "virginica"), c("versicolor", "virginica"))
ggboxplot(iris, x="Species", y="Sepal.Length",color = "Species",palette = "jco", add = "jitter")+
### 添加多組比較的統(tǒng)計(jì)學(xué)結(jié)果
stat_compare_means(label.y = 9.5)+
### 添加每個(gè)兩兩比較的顯著性標(biāo)記位置信息
stat_compare_means(comparisons=my_comparisons, label.y = c(7.6, 8.4, 8.0), label ="p.signif")
該R包可以增加多個(gè)組間的p-value值,還可以增加指定組的組間比較。
相信大家了解了三種添加的方法,如果你們有什么更好的方法或者其他想要分享的,也歡迎給我們投稿。