如何在ggplot2图形上添加显著性差异注释?

时间:2022-07-24
本文章向大家介绍如何在ggplot2图形上添加显著性差异注释?,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

研究者常常要比较两组数据是否有统计学差异,并且要将这种差异在图形上通过线和注释标注出来。

ggplot2包是一个很好的可视化包,ggsignif包是ggplot2包的一个扩展包。

今天来学习怎么在ggplot2包绘制的图形上添加显著性差异注释。

1. 安装R包

install.packages("ggplot2")  # 安装包
install.packages("ggsignif") # 安装包
library(ggplot2) # 加载包
library(ggsignif) # 安装包

2. 加载数据

使用iris数据集。

iris也称鸢尾花卉数据集,包含150个数据样本,分为3类(setosa、versicolour、virginica),每类50个数据,每个数据包含4个属性(花萼长度、花萼宽度、花瓣长度、花瓣宽度)。

data(iris) # 加载数据集
View(iris) # 预览数据集

3. 绘制箱型图

3.1 两组比较

比较数据集中versicolor组和virginica组的Sepal.Length(花萼长度)是否有统计学差异。

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica")))

如上图所示,可以看到两组是有统计学差异的,但是图中的P值使用的是科学计数法,其实还可以使用*或注释来表示。

通过添加参数map_signif_level=TRUE,可以将统计学差异表示为*符号。

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica")),
              map_signif_level=TRUE)

3.2 多组两两比较

还是使用上面的数据集数据。

我们在图上添加3组数据两两比较的统计学差异P值。

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica"),
                                 c("setosa", "virginica"),
                                 c("versicolor", "setosa"))

如上图所示,虽然我们添加了P值,但是P值位置等参数有重叠,需要调整。

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica"),
                                 c("setosa", "virginica"),
                                 c("versicolor", "setosa")),
              y_position=c(8.2, 8.5, 7.5), 
              map_signif_level=TRUE)

y_position中的数字与comparisons中的组别一一对应。

如果我们要调整横线两端的小竖线长度怎么调整?我想要使每根小竖线的长度各不相同

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("setosa", "versicolor"),
                                 c("setosa", "virginica"),
                                 c("versicolor", "virginica")),
              y_position=c(7.5, 8.5, 8.2), 
              tip_length = c(0.2, 0.05, 0.2, 0.05, 0.1, 0.05),
              map_signif_level=TRUE)

主要调整参数就是tip_length()参数,里面的小竖线长度要和组别也是一一对应。

4. 绘制条形图

## 创建数据集
dat <- data.frame(Group = c("S1", "S1", "S2", "S2"),
                  Sub   = c("A", "B", "A", "B"),
                  Value = c(3,5,7,8))  
dat # 查看数据集
ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(y_position=c(6.0, 8.5), xmin=c(0.85, 1.85), xmax=c(1.15, 2.15),
              annotation=c("**", "NS"), tip_length=0.04) + 
  geom_signif(comparisons=list(c("S1", "S2")),
              y_position = 9.3, tip_length = 0.04, vjust=0.2)

请注意:一般根据数据是否符合正态分布,选择合适的统计方法,上面的数据集我统计学方法都是默认的,可以使用函数中的test参数来指定统计学方法。

5. stat_signif()和geom_signif()函数

stat_signif(mapping = NULL, data = NULL, position = "identity",
  na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
  comparisons = NULL, test = "wilcox.test", test.args = NULL,
  annotations = NULL, map_signif_level = FALSE, y_position = NULL,
  xmin = NULL, xmax = NULL, margin_top = 0.05, step_increase = 0,
  tip_length = 0.03, size = 0.5, textsize = 3.88, family = "",
  vjust = 0, parse = FALSE, manual = FALSE, ...)
  
geom_signif(mapping = NULL, data = NULL, stat = "signif",
  position = "identity", na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE, comparisons = NULL, test = "wilcox.test",
  test.args = NULL, annotations = NULL, map_signif_level = FALSE,
  y_position = NULL, xmin = NULL, xmax = NULL, margin_top = 0.05,
  step_increase = 0, tip_length = 0.03, size = 0.5,
  textsize = 3.88, family = "", vjust = 0, parse = FALSE,
  manual = FALSE, ...)
  
## 参数解释
mapping # 由aes()或aes_()创建的美学映射集。如果指定且inherit.aes=TRUE(默认值),它将与绘图顶层的默认映射结合。如果没有绘图映射,则必须提供映射。
data # 绘图数据所在的数据框
position # 位置调整;可以是字符串,也可以是位置调整函数的结果
na.rm # 逻辑词,默认为FALSE,移除缺失值时显示警告信息,为TRUE,则不显示警告信息。
show.legend # 逻辑词,是否显示图例
comparisons # 长度为2的向量列表
test # 进行统计检验的方法名称,如t.test、wilcox.test、aov()、anova()、kruskal.test() 等。
test.args # 检验方法的其他参数
annotations # 替换P值注释的字符向量
map_signif_level # 布尔值,检验结果P值使用注释或者星号代替
y_position # 括号线在对齐y轴高度的数字向量
xmin # 括号线左侧位置的数字向量
xmax # 括号线右侧位置的数字向量
step_increase # 数字向量,减少括号线的重叠
tip_length # 数字向量,显示括号线两端的下降的小竖线,用来指向精确的组别
size # 设置括号线的宽度
textsize # 设置文本字体大小
family # 设置文本字体
vjust # 相对于括号线,上下调整文本的距离
parse # 逻辑词,为TRUE,则标签将解析为表达式

参考资料

  1. stat_signif()和geom_signif()函数帮助文件
  2. https://mp.weixin.qq.com/s/cjeoILJhZhQngXlm2ZZ4Eg