简体   繁体   中英

Heatmap function in R dendrogram failure

For the life of me I cannot understand why this method is failing, I would really appreciate an additional set of eyes here:

heatmap.2(TEST,trace="none",density="none",scale="row", 
     ColSideColors=c("red","blue")[data.test.factors],
     col=redgreen,labRow="", 
     hclustfun=function(x) hclust(x,method="complete"),
     distfun=function(x) as.dist((1 - cor(x))/2))  

The error that I get is: row dendrogram ordering gave index of wrong length

If I don't include the distfun, everything works really well and is responsive to the hclust function. Any advice would be greatly appreicated.

The standard call to dist computes the distance between the rows of the matrix provided, cor computes the correlation between columns of the provided matrix, so the above example to work, you need to transpose the matrix:

heatmap.2(TEST,trace="none",density="none",scale="row", 
     ColSideColors=c("red","blue")[data.test.factors],
     col=redgreen,labRow="", 
     hclustfun=function(x) hclust(x,method="complete"),
     distfun=function(x) as.dist((1 - cor(  t(x)  ))/2))

should work. If you use a square matrix, you'll get code that works, but it won't be calculating what you think it is.

This is not reproducible yet...

 TEST <- matrix(runif(100),nrow=10)
  heatmap.2(TEST, trace="none", density="none", 
            scale="row",
            labRow="",
            hclust=function(x) hclust(x,method="complete"),
            distfun=function(x) as.dist((1-cor(x))/2))

works for me. I don't know what redgreen or data.test.factors are.

Have you tried debug(heatmap.2) or options(error=recover) (or traceback() , although it's unlikely to be useful on its own) to try to track down the precise location of the error?

> sessionInfo()
R version 2.13.0 alpha (2011-03-18 r54865)
Platform: i686-pc-linux-gnu (32-bit)
...
other attached packages:
[1] gplots_2.8.0   caTools_1.12   bitops_1.0-4.1 gdata_2.8.2    gtools_2.6.2  

Building on Ben Bolker's reply, your code seems to work if TEST is an n×n matrix and data.test.factors is a vector of n integers. So for example starting with

 n1 <- 5
 n2 <- 5
 n3 <- 5
 TEST <- matrix(runif(n1*n2), nrow=n1)
 data.test.factors <- sample(n3)

then your code will work. However if n1 and n2 are different then you will get the error row dendrogram ordering gave index of wrong length , while if they are the same but n3 is different or data.test.factors has non-integers then you will get the error 'ColSideColors' must be a character vector of length ncol(x) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM