以鸢尾花数据集为例,展示如何在R上绘制出显著度和p-value。
加载数据集 & 理解数据集
data(iris)
names(iris)
# [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
分析得到,该数据集总共有5个变量,
4个数值型变量:
Sepal.Length
,Sepal.Width
,Petal.Length
,Petal.Width
1个字符串类型的变量:
Species
对分类变量的水平进行查看,得到其总共由3个水平组成:
unique(iris$Species)
# [1] setosa versicolor virginica
# Levels: setosa versicolor virginica
绘制箱线图 & 绘制显著度
选择Sepal.Length
,进行setosa、versicolor、virginica 3者之间的显著性检验。
使用R包:
ggplot2
,ggpubr
以Wilkson秩和检验为例
library(ggplot2)
library(ggpubr)
p_sepallength <- ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(width=0.2, lwd = 1, outlier.size = 3, outlier.alpha = 0.3) +
theme_classic() +
geom_signif(comparisons = list(c("setosa", "versicolor"),
c("setosa", "versicolor"),
c("versicolor", "virginica")),
map_signif_level=FALSE,
textsize=5,
test=wilcox.test,
step_increase=0.2,
size = 1) +
theme(axis.line = element_line(size = 0.5),
axis.ticks = element_line(size = 1),
axis.text = element_text(size = 16),
axis.title = element_text(size = 16))
p_sepallength
# ggsave('2022-6-17-iris-significance-test.png', p_sepallength, width = 6, height = 6, dpi = 600)
结果图1:
结果图2(将p-value改为显著度标识):
将上述代码中的
map_signif_level=FALSE
改为map_signif_level=TRUE