[R] Cutting heatmap dendrogram

Martin Maechler maechler at stat.math.ethz.ch
Wed Jul 21 18:41:31 CEST 2004


>>>>> "paul" == paul boutros <paul.boutros at utoronto.ca>
>>>>>     on Wed, 21 Jul 2004 12:03:39 -0400 writes:

    paul> Hi Sean, Martin
    >>>>>>> "Sean" == Sean Davis <sdavis2 at mail.nih.gov>
    >>>>>>> on Wed, 21 Jul 2004 06:01:33 -0400 writes:

    Sean> Paul, You can certainly get a heatmap of a subset of
    Sean> your data by simply subsetting.  If you have a group
    Sean> of genes obtained from cutree, simply do a heatmap on
    Sean> that set of genes.  If you obtain a set of genes, say
    Sean> A, and want to do a heatmap on that subset, simply do
    Sean> heatmap(as.matrix(data[A,])) where A contains either a
    Sean> logical vector or indices for the genes of interest.
    Sean> The dendrograms will be generated for those samples
    Sean> and genes based on the subset of data.  You could, of
    Sean> course, pass in the sample dendrogram from the
    Sean> original clustering of all genes if you like.

    >> this was probably helpful but didn't really answer the original question.

    paul> Yeah, I'm okay with getting subsets and plotting them
    paul> separately, but the goal is to be able to
    paul> automatically show and label the subsets on the
    paul> heatmap itself.

    >> One thing you (Paul) should do is to "cut()" the dendrogram
    >> instead of "cutree()"ing the hclust result and then *pass* the
    >> cut()ed dendrogram directly to heatmap(). 

    paul> Ahh, that makes sense, but maybe I'm missing something?

yes.  The fact (that I also forget to mention in my reply above,
but clearly visible from  ?cut.dendrogram )
that  cut(<dendrogram>) returns a  list() , not a dendrogram
and from that list, you are really interested in the $upper
component which *is* a dendrogram.

I.e., in your example below
if you replace

    > samples.dendrogr2 <- cut(samples.dendrogr1, 4);
by
    > samples.dendrogr2 <- cut(samples.dendrogr1, 4)$upper

'samples.dendrogr2' *is* a dendrogram.

However, I don't think that it will currently work directly with
the current version of heatmap()
*and* the original data matrix.

Enhancements should be possible, but not today
(and there are some from "heatmap2" that are *still* not in
 R-devel's heatmap() function !).

Regards,
Martin

    paul> ###########################################
    >> samples.dendrogr2 <- cut(samples.dendrogr1, 4);
    >> samples.dendrogr2
    paul> $upper
    paul> `dendrogram' with 2 branches and 2 members total, at height 1.082017 

    paul> $lower
    paul> $lower[[1]]
    paul> `dendrogram' with 2 branches and 4 members total, at height 0.0631504 

    paul> $lower[[2]]
    paul> `dendrogram' with 2 branches and 12 members total, at height 0.5823986 


    >> plot(samples.dendrogr2);
    paul> Error in plot.window(xlim, ylim, log, asp, ...) : 
    paul> need finite xlim values
    paul> In addition: Warning messages: 
    paul> 1: no finite arguments to min; returning Inf 
    paul> 2: no finite arguments to max; returning -Inf 
    paul> 3: no finite arguments to min; returning Inf 
    paul> 4: no finite arguments to max; returning -Inf

    >> hv <- heatmap(as.matrix(data), Rowv=genes.dendrogr, Colv=samples.dendrogr2); 
    paul> Error in lV + rV : non-numeric argument to binary operator

    >> traceback();
    paul> 8: oV(x[[1]], wts)
    paul> 7: oV(x[[1]], wts)
    paul> 6: oV(x, wts)
    paul> 5: inherits(x, "dendrogram")
    paul> 4: midcache.dendrogram(oV(x, wts))
    paul> 3: reorder.dendrogram(ddc, Colv)
    paul> 2: reorder(ddc, Colv)
    paul> 1: heatmap(as.matrix(data), Rowv = genes.dendrogr, Colv = samples.dendrogr2, 
    paul> col = my.colors())
    paul> ###########################################

    paul> Any ideas are (still) very much appreciated!
    paul> Paul

    >> I'm interested to hear if that works (haven't got time to
    >> experiment with that just now).

    Sean> It sounds like you are aiming for interactive
    Sean> clustering, which R does not do well.  Consider using
    Sean> an external viewer such as the cluster/treeview combo
    Sean> or the TIGR clustering program (can't remember name).

    paul> I'll check out MeV, then, but since the rest of my processing is in R....

    Sean> Finally, for future reference, it is probably worthwhile posting 
    Sean> microarray questions to the Bioconductor mailing list rather than 
    Sean> R-Help.

    >> I disagree.  This was a question about heatmap() an R function
    >> of more general use than microarrays.
    >> I would have found it "wrong" to ask this question on the
    >> bioconductor mailing list.

    >> Martin Maechler

    Sean> Sean

    Sean> On Jul 20, 2004, at 9:39 PM, Paul Boutros wrote:

    >>> Hello,
    >>> 
    >>> I've been clustering my data using hclust and cutting the resulting 
    >>> tree
    >>> with cutree.  Separately, I visualize the clusterings with heatmap.  
    >>> Is it
    >>> possible to have the dendrogram on the heatmap reflect the cutree 
    >>> results?
    >>> That is, instead of having one large dendrogram, it would have 4 or 25 
    >>> in
    >>> the example below.  Any guidance on if that's possible or not, and what
    >>> kinds of commands I should be looking into would be very much 
    >>> appreciated.
    >>> I'm using R 1.9.0 on Windows XP.
    >>> 
    >>> Thanks!
    >>> Paul
    >>> 
    >>> # load libraries
    >>> library(stats);
    >>> 
    >>> # working copy of data
    >>> set1 <- as.matrix(data);
    >>> set2 <- t(set1);
    >>> 
    >>> # genes
    >>> genes.distance <- as.dist(1-cor(set2));
    >>> genes.clusters <- hclust(genes.distance);
    >>> genes.dendrogr <- as.dendrogram(genes.clusters);
    >>> 
    >>> # samples
    >>> samples.distance <- as.dist(1-cor(set1));
    >>> samples.clusters <- hclust(samples.distance1);
    >>> samples.dendrogr <- as.dendrogram(samples.clusters1);
    >>> 
    >>> # cut the trees
    >>> samples.members  <- cutree(samples.clusters, k=4);
    >>> genes.members    <- cutree(genes.clusters,   k=25);
    >>> 
    >>> # heatmap colouring
    >>> my.colors <- function(n = 20, low.col = 0.35, high.col=1, saturation = 
    >>> 0.85)
    >>> {
    >>> if (n < 2) stop("n must be greater than 2")
    >>> n1 <- n%/%2
    >>> n2 <- n - n1
    >>> c(hsv(low.col, saturation, seq(1,0,length=n1)),
    >>> hsv(high.col, saturation, seq(0,1,length=n2)))
    >>> }
    >>> 
    >>> # make the heatmap
    >>> hv <- heatmap(as.matrix(data), Rowv=genes.dendrogr, 
    >>> Colv=samples.dendrogr,
    >>> col=my.colors());
    >>>




More information about the R-help mailing list