1

I'm really newbie in R and I'd like to use it to carry out a co-occurrence analysis of microbial taxa. I have a table like this (tab-separated) with the relative abundance of taxa:

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

I'd like to obtain a Spearman's rank correlation matrix of the taxa and then plot it in some nice graph. I'm supposed to have to convert my table in matrix, before running the corr.test command, right?? So, I tryed to convert it and it didn't give me any error, but when I tryed to tun the corr.test, it says that the matrix is not numeric....

Can anyone help me to figure out how to do??

Thanks Francesca

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • This sounds like homework, and you have not shown the code or the outout of `str()` on the data. – IRTFM Oct 01 '13 at 21:21
  • Hi DWin! I'm sorry, I don't know the forum rules... These are the command I ran:> row.names(otu.tab1)<-otu_tab1 $Taxon > row.names(otu_tab1)<-otu_tab1 $Taxon > dim(otu_tab1) [1] 134 54 > otu_tab1<-otu_tab1[,2:54] > otu_tab1_mat<-as.matrix(otu_tab1) And this is the output of str():> str(otu_tab1_mat) chr [1:134, 1:53] "0.0083825900" "0.0000000000" "0.0000000000" ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:134] "Root;Other;Other;Other" "Chloracidobacteria;f__" "Solibacteres;f__Solibacteraceae" "Actinobacteria;Other" ... ..$ : chr [1:53] "NO8.4" "NO6.1" "NO7.3" "NO9.4" ... – Francesca de Filippis Oct 01 '13 at 21:48
  • 2
    Noooooo. Edit your question. Please do some more reading of the instructions for using this website. – IRTFM Oct 01 '13 at 21:53
  • Welcome to stackoverflow! You find some info [about Stackoverflow here](http://stackoverflow.com/about) and tips on [how to ask here](http://meta.stackoverflow.com/help/how-to-ask). You are much more likely to receive an answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Oct 01 '13 at 23:01

1 Answers1

0

I just figured out how to do, so I'm postinf the answer to my own question, hoping it could be useful to others:

    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")
MattBagg
  • 10,268
  • 3
  • 40
  • 47