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

From: Wittner, Ben <Wittner.Ben_at_mgh.harvard.edu>
Date: Mon 29 Aug 2005 - 23:37:45 EST


Thanks Paul.

It seems there's an undocumented requirement that in order to use a dendrogram as an argument to heatmap(),
(a) the leaf nodes must be integers indicating the leaf's position in the left-to-right ordering of the leafs and/or (b) only the root of the dendrogram can be of class dendrogram.

I discovered this by doing as you suggested below and with some help from Jeff Gentry.

-Ben

> -----Original Message-----
> From: Paul Murrell [mailto:p.murrell@auckland.ac.nz]
> Sent: Thursday, August 25, 2005 7:37 PM
> To: Wittner, Ben
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] 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@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@stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/



R-help@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 Received on Mon Aug 29 23:43:29 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:01:55 EST