[R] problem building dendrograms to use with heatmap()

Wittner, Ben Wittner.Ben at mgh.harvard.edu
Mon Aug 22 23:48:03 CEST 2005


Hi,

I'm trying to build dendrograms to pass to heatmap().
The dendrograms I build plot properly, but when I pass them to heatmap() I get
the error message "row dendrogram ordering gave index of wrong length" (see
output log below).
I looked in the code of heatmap() and saw that the error was due to a NULL
return value from order.dendrogram(), which in turn got a NULL return value from
unlist(). But I have no idea why unlist() is returning NULL.

I've included code below which reproduces the problem and below that the output
from a run of that code on my computer.

Any help would be greatly appreciated. Thanks in advance.

-Ben

###########################  begin code  ###################################

version

dendro.leaf <- function(label) {
  ans <- list()
  attr(ans, 'members') <- 1
  attr(ans, 'height') <- 0
  attr(ans, 'leaf') <- T
  attr(ans, 'midpoint') <- 0
  attr(ans, 'label') <- label
  attr(ans, 'class') <- 'dendrogram'
  ans
}

dendro.merge <- function(d1, d2, height) {
  ans <- list(d1, d2)
  members <- attr(d1, 'members') + attr(d2, 'members')
  attr(ans, 'members') <- members
  attr(ans, 'height') <- height
  attr(ans, 'leaf') <- F
  attr(ans, 'midpoint') <- (members - 1)/2
  attr(ans, 'class') <- 'dendrogram'
  ans
}

lc1 <- dendro.leaf('c1')
lc2 <- dendro.leaf('c2')
lc3 <- dendro.leaf('c3')
nc1 <- dendro.merge(lc1, lc2, 0.1)
nc2 <- dendro.merge(nc1, lc3, 0.2)
plot(nc2)

lr1 <- dendro.leaf('r1')
lr2 <- dendro.leaf('r2')
lr3 <- dendro.leaf('r3')
nr1 <- dendro.merge(lr2, lr3, 0.1)
nr2 <- dendro.merge(lr1, nr1, 0.3)
plot(nr2)

x <- matrix(seq(-1, 1, length.out=9), nrow=3)
rownames(x) <- paste('r', 1:3, sep='')
colnames(x) <- paste('c', 1:3, sep='')

heatmap(x, Rowv=nr2, Colv=nc2, scale='none')

order.dendrogram(nr2)

unlist(nr2)

###############  begin output from run of code above  ##################

> version
         _                
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    2                
minor    1.1              
year     2005             
month    06               
day      20               
language R                
> 
> dendro.leaf <- function(label) {
+   ans <- list()
+   attr(ans, 'members') <- 1
+   attr(ans, 'height') <- 0
+   attr(ans, 'leaf') <- T
+   attr(ans, 'midpoint') <- 0
+   attr(ans, 'label') <- label
+   attr(ans, 'class') <- 'dendrogram'
+   ans
+ }
> 
> dendro.merge <- function(d1, d2, height) {
+   ans <- list(d1, d2)
+   members <- attr(d1, 'members') + attr(d2, 'members')
+   attr(ans, 'members') <- members
+   attr(ans, 'height') <- height
+   attr(ans, 'leaf') <- F
+   attr(ans, 'midpoint') <- (members - 1)/2
+   attr(ans, 'class') <- 'dendrogram'
+   ans
+ }
> 
> lc1 <- dendro.leaf('c1')
> lc2 <- dendro.leaf('c2')
> lc3 <- dendro.leaf('c3')
> nc1 <- dendro.merge(lc1, lc2, 0.1)
> nc2 <- dendro.merge(nc1, lc3, 0.2)
> plot(nc2)
> 
> lr1 <- dendro.leaf('r1')
> lr2 <- dendro.leaf('r2')
> lr3 <- dendro.leaf('r3')
> nr1 <- dendro.merge(lr2, lr3, 0.1)
> nr2 <- dendro.merge(lr1, nr1, 0.3)
> plot(nr2)
> 
> x <- matrix(seq(-1, 1, length.out=9), nrow=3)
> rownames(x) <- paste('r', 1:3, sep='')
> colnames(x) <- paste('c', 1:3, sep='')
> 
> heatmap(x, Rowv=nr2, Colv=nc2, scale='none')
Error in heatmap(x, Rowv = nr2, Colv = nc2, scale = "none") : 
        row dendrogram ordering gave index of wrong length
> 
> order.dendrogram(nr2)
NULL
> 
> unlist(nr2)
NULL
>




More information about the R-help mailing list