差异分析完整解决方案升级版

我看见过最全的差异分析教程,但是好像还没有最智能的,机械的差异分析教程。因为这些标准都是死的,我们完全可以使用if来解放我们的大脑。

之前我有一篇推送: R语言一键批量完成差异统计和可视化(https://mp.weixin.qq.com/s/OrAx7ueiDa-O_pvpsQICGA/) 提供了这个差异检测编码流程,但是做的比较粗糙,代码冗余过多,出现错误向上排查比较困难。所以这次我将本来1000行的代码修改到了300行,精简了许多。

一张神图,解决科研统计80%的问题(https://mp.weixin.qq.com/s/OhkSFJSUcgDD9nFdc8kHTg)。 如果如作者所言:神图,解决科研统计80%的问题,那么我的这个流程把这张图90%的内容都进行了自动化数据识别和自动分析。顺便画张图,还有两种出图方式任你选择(建议箱线图,更准确)。

同时有几个地方我进行了升级:

  1. aov添加多种多重比较的方法,并映射到图形上。

  2. 提供两种作图方式,两种差异展示方式。

  3. 提供表格形式的结果文件方便查看。

这次代码升级有几个地方需要认真编写:

  1. 柱状图的差异分析line形式的差异自动添加:如图:

    这里我还是本来想使用ggpubr进行做的,发现这里面根本就没有柱状图添加line形式的差异的方法,最后还是使用了ggsignif包,其实就这一个包就够了。

if (sig_show == "line") {
zuhe = combn(aa$group,2)
xxxx <- tapply(zuhe,rep(1:ncol(zuhe),each=nrow(zuhe)),function(i)i)
xxxx
sig_lis = rep("a",dim(zuhe)[2])
for (i in 1:dim(zuhe)[2]) {
library(tidyverse)
library("ggsignif")

if (filter(aa, group == xxxx[[i]][1])$groups == filter(aa, group == xxxx[[i]][2])$groups) {
sig_lis[i] = "no_sig"
}

if (filter(aa, group == xxxx[[i]][1])$groups != filter(aa, group == xxxx[[i]][2])$groups) {
sig_lis[i] = "*"
}

}

  1. 两两非参数检验结果是成对的,之前我可能提高多如何将成对差异结果表示为字母,这是本次升级的又一个技巧:multcompLetters函数。

names(wilcox_levels) = paste(xx$group1,xx$group2,sep = "-")

wilcox.labels <- data.frame(multcompLetters(wilcox_levels, threshold = 0.05)['Letters'])
colnames(wilcox.labels) = "groups"
aa = wilcox.labels

  1. 那如果将字母形式的显著性标记结果转化为两两比较的结果呢,这里有我自己编写了一个脚本,做了这个转化;

zuhe = combn(aa$group,2)
xxxx <- tapply(zuhe,rep(1:ncol(zuhe),each=nrow(zuhe)),function(i)i)
xxxx
sig_lis = rep("a",dim(zuhe)[2])
for (i in 1:dim(zuhe)[2]) {
library(tidyverse)
library("ggsignif")

if (filter(aa, group == xxxx[[i]][1])$groups == filter(aa, group == xxxx[[i]][2])$groups) {
sig_lis[i] = "no_sig"
}

if (filter(aa, group == xxxx[[i]][1])$groups != filter(aa, group == xxxx[[i]][2])$groups) {
sig_lis[i] = "*"
}

}

到这里我们似乎就没什么难度了,将整个函数编写完毕,运行不同的参数进行调试即可:

导入数据

# 读入实验设计
data_wt = read.table("./cs.txt", header=T, sep="\t");head(data_wt)
##数据由长变宽
data_wt = dcast(data_wt,ID +group ~ grou, value.var = "count")
#这里备注所需的数据格式
#前量列从第一列开始是ID,第二列是分组信息,剩下的列均为数据列
head(data_wt)

下面运行函数,首先测试五种多重比较方法


single_diff_and_plot (data_wt,plotname = "南京农业大学_",plot = "bar",method_Mc ="LSD")
single_diff_and_plot (data_wt,plotname = "南京农业大学_",plot = "bar",method_Mc ="scheffe")
single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc ="LSD" )
single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc = "scheffe" )
method_Mc == "Duncan"
single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc = "Duncan" )

接着测试显著性标注方式

single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc = "Duncan",sig_show = "abc" )

single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc = "Duncan",sig_show = "line" )
single_diff_and_plot (data_wt,plotname = "资源与环境科学学院_",plot = "box",method_Mc = "Duncan",sig_show = "abc" )

函数默认参数如下:

data_wt = data_wt,##默认数据集
plotname = "wentao_",#出图同意带有这个前缀
plot = "bar",#出图形式为bar

#method_cv: 方差齐心方法选择,有两种方法:method_cv == "leveneTest",method_cv == "bartlett.test"
method_cv = "bartlett.test"
,
method_Mc = "Tukey",
sig_show = "abc"

成图和差异表格列表欣赏:

函数主体

(0)

相关推荐