From maechler at stat.math.ethz.ch  Mon Mar 12 18:42:32 2012
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 12 Mar 2012 18:42:32 +0100
Subject: [Rd] Unexpected behaviour for RowSideColors in function heatmap
In-Reply-To: <0ADEEA73-BA4E-46FA-9891-589B7B644389@ebi.ac.uk>
References: <2357666E-86E2-4805-AD50-010588BE11B7@ebi.ac.uk>
	<4F58EBEC.6000004@gmail.com>
	<0ADEEA73-BA4E-46FA-9891-589B7B644389@ebi.ac.uk>
Message-ID: <20318.13704.600355.728549@stat.math.ethz.ch>


> Thanks Kevin, I experienced the same problems with heatmap.2, and therefore resorted to heatmap. For heatmap, the code change I suggested below fixes the problem with side color ordering as far as I can tell, and lets one create a symmetric heatmap with the origin at the top left (which I agree makes it easier to read). Unless this code change breaks something else, it would be nice if it could be incorporated into R.

It now has been incorporated:
For 2.15.0 (due in less than three weeks, see "Release Plans" on
    http://developer.r-project.org/)

Thanks you very much, P?r (and Kevin) !

Martin

> P?r

> On 8 Mar 2012, at 17:27, Kevin R. Coombes wrote:

> > First, I can confirm this problem exists today, and can now vaguely recall seeing it in previous version of R.
> > > sessionInfo()
> > R version 2.14.1 (2011-12-22)
> > Platform: x86_64-pc-mingw32/x64 (64-bit)
> >
> > There is a bigger problem with heatmap.2 in the gplots package.  Using "symm=TRUE" with no other arguments causes it to fail to print one of the dendrograms:
> > > library(gplots)
> > > cU <- cor(USJudgeRatings)
> > > heatmap.2(cU, symm = TRUE)
> > Warning message:
> > In heatmap.2(cU, symm = TRUE) :
> >   Discrepancy: Colv is FALSE, while dendrogram is `row'. Omitting column dendogram.
> >
> > In order to get the column dendrogram displayed using heatmap.2, you must use "symm=TRUE, Colv=TRUE".  So this succeeds:
> > >  heatmap.2(cU, symm = TRUE, Colv=TRUE)
> >
> > To make things worse, heatmap.2 sometimes gets the direction of the column colors wrong.  If you omit "Colv=TRUE", it leaves out the column dendrogram and gets one set of colors wrong.  That, is, the following command fails in two ways:
> > > sideCols <- rainbow(ncol(cU))
> > > heatmap.2(cU, symm = TRUE, distfun = function(c) as.dist(1 - c),
> >        ColSideColors=sideCols, RowSideColors=sideCols)
> >
> > If you include "Colv=TRUE", then it prints the dendrogram and gets both sets of colorbars correct.  So the following command succeeds:
> > > heatmap.2(cU, symm = TRUE, Colv=TRUE, distfun = function(c) as.dist(1 - c),
> > +        ColSideColors=sideCols, RowSideColors=sideCols)
> >
> > It is a separate esthetic question as to which diagonal axis should be used for symmetry. When it works in heatmap.2, the "origin" is at the bottom left.  Since the dendrograms are printed on the top and left, however, the symmetry would be easier to see/confirm if the origin were put at the top left. One might be tempted to fit this by using "revC=TRUE".  However, this command fails because it gets the colorbar wrong:
> > > heatmap.2(cU, symm = TRUE, Colv=TRUE, revC=TRUE,
> >           distfun = function(c) as.dist(1 - c),
> >           ColSideColors=sideCols, RowSideColors=sideCols)
> >
> > I am occasionally of the opinion that both "heatmap" and "heatmap.2" are too complex to be used reliably by mortals....
> >     Kevin
> >
> > On 2/28/2012 4:04 AM, P?r Engstr?m wrote:
> >>
> >> Hello,
> >>
> >> I have come across some unexpected behaviour of the function heatmap in the stats package. This looks like a bug to me, but I might have misunderstood something.
> >>
> >> When calling the function in symmetric mode, the ColSideColors are plotted correctly, but RowSideColors appear in reverse order. This code (modified from the example on the help page) demonstrates the problem:
> >>
> >> cU <- cor(USJudgeRatings)
> >> sideCols <- rainbow(ncol(cU))
> >> heatmap(cU, symm = TRUE, distfun = function(c) as.dist(1 - c),
> >>        ColSideColors=sideCols, RowSideColors=sideCols)
> >>
> >> Reversing the RowSideColors argument does not solve the problem:
> >>
> >> heatmap(cU, symm = TRUE, distfun = function(c) as.dist(1 - c),
> >>        ColSideColors=sideCols, RowSideColors=rev(sideCols))
> >>
> >> I had a look at the the function code and found that this change fixes the problem:
> >>
> >> Replace
> >>
> >>        image(rbind(1L:nr), col = RowSideColors[rowInd], axes = FALSE)
> >>
> >> with
> >>
> >>        image(rbind(if(revC) nr:1L else 1L:nr), col = RowSideColors[rowInd], axes = FALSE)
> >>
> >> I am using the current version of R for Mac OS X:
> >>
> >>> sessionInfo()
> >> R version 2.14.1 (2011-12-22)
> >> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
> >>
> >> locale:
> >> [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
> >>
> >> attached base packages:
> >> [1] stats     graphics  grDevices utils     datasets  methods   base
> >>
> >>
> >> Regards,
> >>
> >> P?r Engstr?m
> >>
> >> Postdoctoral Fellow
> >> EMBL European Bioinformatics Institute
> >> Wellcome Trust Genome Campus
> >> Hinxton, Cambridge, UK



[Rd] Unexpected behaviour for RowSideColors in function heatmap
Martin Maechler