如何用R语言进行Pvalue显著性标记?
February 19th, 2022

箱线图是统计学中较常见的图形之一。这篇文章将讲述如何简单比较两组或多组的平均值,且添加显著性标记。

通常情况根据显著性p值的数值大小,分为四类:
(1)0.01≤p<0.05,*
(2)0.001≤p<0.01,**
(3)0.0001≤p<0.001,***
(4)p<0.0001, ****
接下来会讲述三种添加显著性标记的方法。
方法1-手动添加

1:创建数据,绘制箱线图。
library(ggplot2)
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_boxplot()

2:手动添加显著性标记。
library(ggplot2)

设置横线的四个点的位置

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()+

按照设定的位置绘制线

geom_line(data = df2, aes(x = a, y = b)) +

添加显著性标记信息

annotate("text", x = 2.5, y = 8.2, label = "***", size = 8)

该方法的缺点是:在手动添加显著性时,需要确认显著性的P值的大小。
方法2-使用ggsignif

1:下载并安装ggsignif包。
install.packages("ggsignif")

2:创建数据,绘制箱线图。
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()

3:利用ggsignif R包添加显著性标记。
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:创建数据,绘制箱线图。
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包添加显著性标记。
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")+

添加多组比较的统计学结果

stat_compare_means(label.y = 9.5)+

添加每个两两比较的显著性标记位置信息

stat_compare_means(comparisons=my_comparisons, label.y = c(7.6, 8.4, 8.0), label ="p.signif")

该R包可以增加多个组间的p-value值,还可以增加指定组的组间比较。

Subscribe to JackWu
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from JackWu

Skeleton

Skeleton

Skeleton