Cook R科研信息学

ggpubr添加组间比较参数到图中

2020-12-15  本文已影响0人  灵活胖子的进步之路

这次我们用的的数据集是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检验
#更改检验方法为配对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的逻辑值为真

配对T检验
# 三组比较,两两比较的实现方法
#首先,构建如下列表,列表里面是想要比较组的组件名称,这里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轴的位置
3组间相互比较 比较方法选择,不会的同学可以自行百度哈
# 设定参考组后进行多组比较(均和参考组进行比较)
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")#设定比较方法及设定参考组

设定参考组后组间比较的展示图 ase映射P值的可选参数
上一篇 下一篇

猜你喜欢

热点阅读