problem building dendrograms to use with heatmap()
Hi
Wittner, Ben wrote:
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).
Looks like you're not building the dendrograms "properly". Compare unclass(nr2) with unclass() of a dendrogram from as.dendrogram(hclust(<something>)). You might need to look closely at stats:::as.dendrogram.hclust to get it right. Paul
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
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/