这次我们用的的数据集是ToothGrowth数据集,以下是数据集基本介绍
Description
The response is the length of odontoblasts (cells responsible for tooth growth) in 60 guinea pigs. Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, orange juice or ascorbic acid (a form of vitamin C and coded as VC).
Usage
ToothGrowth
Format
A data frame with 60 observations on 3 variables.
[,1] len numeric Tooth length
[,2] supp factor Supplement type (VC or OJ).
[,3] dose numeric Dose in milligrams/day
可以看到,这是一个不同药物剂量及剂型对于某类猪牙齿生长的影响,len为连续性变量,为牙齿生长长度;sup为给予方式,分类变量;dose 为数值型变量,不过他仅有三个数值 0.5, 1, and 2 mg/day,其可以当成分类变量
#首先载入包及展示数据集
library(ggpubr)
data("ToothGrowth")
str(ToothGrowth)
# 两组的比较
p <- ggboxplot(ToothGrowth,#数据集
x = "supp", #横坐标分组
y = "len",#纵坐标向量
color = "supp",#分组线段颜色
palette = "npg", #色板情况,
add = "jitter")#增加不重叠抖动点
# #增加均值比较
p + stat_compare_means()
# 更改比较方法为t检验
p + stat_compare_means(method = "t.test")
#更改检验方法为配对T检验,绘图函数为ggpaired
ggpaired(ToothGrowth,
x = "supp",
y = "len",
color = "supp",
line.color = "gray", #配对连线的颜色
line.size = 0.4,#配对连线的大小
palette = "npg")+
stat_compare_means(paired = TRUE)#这里需要制定paired的逻辑值为真
# 三组比较,两两比较的实现方法
#首先,构建如下列表,列表里面是想要比较组的组件名称,这里0.5,1,2均为x轴dose的名称
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth,
x = "dose",
y = "len",
color = "dose",
palette = "npg",
add = "jitter",
shape = "dose")+
stat_compare_means(comparisons = my_comparisons,#刚才构建的列表
label.y = c(29, 35, 40))+#显示P值在y轴上的位置
stat_compare_means(label.y = 45) # 添加总体组件比较的P值并确定在y轴的位置
# 设定参考组后进行多组比较(均和参考组进行比较)
ggboxplot(ToothGrowth,
x = "dose",
y = "len",
color = "dose",
palette = "jama")+#换个JAMA的色板
stat_compare_means(method = "anova", #确定多组比较方法学
label.y = 40)+ # 确定显示p值纵坐标
stat_compare_means(aes(label = ..p.signif..),#不显示具体P指而进显示其显著性水平
method = "t.test", ref.group = "0.5")#设定比较方法及设定参考组