R矩阵和共生分析

时间:2013-10-01 21:12:09

标签: r matrix correlation

我真的是R的新手,我想用它来进行微生物类群的共现分析。我有一个像这样的表(制表符分隔)与相对丰富的分类群:

Taxon Sample1 Sample2 Sample3.......Sample54
OTU1    0.2     0.005   0.009         0.12
OTU2    0.62.....
OTU3
....
OTU136

我想获得分类群的Spearman等级相关矩阵,然后在一些漂亮的图中绘制它。在运行corr.test命令之前,我应该在矩阵中转换我的表,对吧?所以,我尝试转换它并没有给我任何错误,但是当我试图调整corr.test时,它说矩阵不是数字....

任何人都可以帮我弄清楚怎么办?

由于 弗朗西斯

1 个答案:

答案 0 :(得分:0)

我刚想出怎么做,所以我发布了自己的问题的答案,希望它对其他人有用:

    require(gplots)  # for heatmap.2()

    mydata <- read.csv(file="otu_tab_L4.txt", header=T, row.names=1, sep="\t")

    #transposes it and makes a matrix
    mydata_t <- t(as.matrix(mydata))

    #makes Spearman's correlation
    cor.matrix <- cor(mydata_t, method = "spearman")

    #remove upper part of the matrix, since it gives the same informations 
    cor.matrix[upper.tri(cor.matrix )] <- NA

    #checks matrix dimensions
    dim(cor.matrix)

    #Finally, I plotted a triangular heatmap of my matrix
    ##the standard function will put the y labels on the right!! I wanted them on the         left! So ##changed the heatmap.2 function code in this way:

    ####open the code:
    fix(heatmap.2)

    ###modify it in this way:
    ##when it says: " axis(4, iy, labels = labRow, las = 2, line = -0.5, tick = 0, 
     cex.axis = cexRow) "
    ##I put 2 in place of 4!! 

    heatmap<-      heatmap.2(cor.matrix,scale="none",Rowv=NA,Colv=NA,col=rainbow,margins(5,5),cexRow=0.5,cexCol=1.0,key=TRUE,keysize=1.5,trace="none")