heatmap 'contrast': suggestions for a zlim adjustment in heatmap.2
1
0
Entering edit mode
k. brand ▴ 420
@k-brand-1874
Last seen 10.3 years ago
Esteemed BioC Users, In pursuit of a simple method to adjust the "contrast" of a heatmap i attempted to use the image() argument "zlim" in heatmap.2, both directly and using add.expr. These attempts failed (shown below on my example heatmap.2). heatplot() from library(made4) includes just such a convenient zlim= argument, but for now i'd trying to use heatmap.2 for all my heatmap needs. For the record, i can achieve what a zlim adjustment using the breaks argument to specify the effective range of zlim, but specifying breaks interferes with the scale argument in heatmap.2. So no good. So any suggestions for a zlim adjustment in heatmap.2 are greatly appreciated, cheers, Karl #example toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) library(gplots) windows() plot.new() heatmap.2(toy.mat, Rowv=T, Colv=T, dendrogram="column", scale= "row", col=c(greenred(256)), # breaks = c(seq(-10, 10, length.out = 257)),#interferes with 'scale' trace="none", density.info="none", zlim=c(-10, 10) #no error, but no effect # add.expr = (zlim=c(-10, 10)) #no error, but no effect # add.expr = "zlim", zlim=c(-10, 10) #no error, but no effect # add.expr = zlim, zlim=c(-10, 10) #produces error ) #end -- Karl Brand <k.brand at="" erasmusmc.nl=""> Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
• 3.0k views
ADD COMMENT
0
Entering edit mode
Kevin Coombes ▴ 430
@kevin-coombes-3935
Last seen 2.2 years ago
United States
I've had the best results by treating heatmap as purely a display function, and breaking out all of the processing in previous steps. To make that more explicit, you can do something like rowclust <- hclust(mydDist(mat, "myMetric"), "myLinkage") colclust <- hclust(myDist(mat, "anotherMetric"), "anotherLinkage") stdizedmat <- t(scale(t(mat))) clipper <- 3 stdizedmat[stdizedmat < -clipper]<- -clipper stdidezmat[stdizedmat > clipper] <- clipper heatmap(stdizedmat, Rowv=as.dendrogram(rowclust), Colv=as.dendrogram(colclust), scale='none', zlim=c(-clipper, clipper)) Karl Brand wrote: > Esteemed BioC Users, > > In pursuit of a simple method to adjust the "contrast" of a heatmap i > attempted to use the image() argument "zlim" in heatmap.2, both > directly and using add.expr. These attempts failed (shown below on my > example heatmap.2). > > heatplot() from library(made4) includes just such a convenient zlim= > argument, but for now i'd trying to use heatmap.2 for all my heatmap > needs. For the record, i can achieve what a zlim adjustment using the > breaks argument to specify the effective range of zlim, but specifying > breaks interferes with the scale argument in heatmap.2. So no good. > > So any suggestions for a zlim adjustment in heatmap.2 are greatly > appreciated, cheers, > > Karl > > > > #example > toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) > library(gplots) > windows() > plot.new() > heatmap.2(toy.mat, > Rowv=T, Colv=T, > dendrogram="column", > scale= "row", > col=c(greenred(256)), > # breaks = c(seq(-10, 10, length.out = 257)),#interferes with 'scale' > trace="none", > density.info="none", > zlim=c(-10, 10) #no error, but no effect > # add.expr = (zlim=c(-10, 10)) #no error, but no effect > # add.expr = "zlim", zlim=c(-10, 10) #no error, but no effect > # add.expr = zlim, zlim=c(-10, 10) #produces error > ) > #end >
ADD COMMENT
0
Entering edit mode
Thank you Kevin, I took a punt on your suggestion which is very helpful given that i need more control over the details of my plots. But that also means i will need more understanding about what i'm doing and how to do it. Which i lack. I failed to duplicate the clustering i achieved within heatmap.2 when attempting to cluster 'manually'. The plots (run examples below) differ in row ordering. So far i cant work out how to eliminate this difference. Any help clarifying whats needed to manually duplicate the row ordering greatly appreciated, Karl ##run toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) #cluster manually then just plot with heatmap.2 rowclust <- hclust(as.dist(1-cor(t(toy.mat))), method="average") colclust <- hclust(as.dist(1-cor(toy.mat)), method="average") stdizedmat <- t(scale(t(toy.mat))) clipper <- 2 stdizedmat[stdizedmat < -clipper]<- -clipper stdizedmat[stdizedmat > clipper] <- clipper windows() plot.new() heatmap.2(stdizedmat, Rowv=as.dendrogram(rowclust), Colv=as.dendrogram(colclust), scale="none", zlim=c(-clipper, clipper), col=greenred(256), trace="none", density.info="none", main="manually clustered" ) #using heatmap.2 to cluster AND plot dist.pear <- function(x) as.dist(1-cor(t(x))) hclust.ave <- function(x) hclust(x, method="average") windows() plot.new() heatmap.2(toy.mat, Rowv=T, Colv=T, distfun=dist.pear, hclustfun=hclust.ave, scale= "row", col=greenred(256), breaks = c(seq(-2, 2, length.out = 257)), trace="none", density.info="none", main="heatmap.2 clustered" ) ##end On 8/19/2010 11:13 PM, Kevin Coombes wrote: > I've had the best results by treating heatmap as purely a display > function, and breaking out all of the processing in previous steps. To > make that more explicit, you can do something like > > rowclust <- hclust(mydDist(mat, "myMetric"), "myLinkage") > colclust <- hclust(myDist(mat, "anotherMetric"), "anotherLinkage") > stdizedmat <- t(scale(t(mat))) > clipper <- 3 > stdizedmat[stdizedmat < -clipper]<- -clipper > stdidezmat[stdizedmat > clipper] <- clipper > > heatmap(stdizedmat, > Rowv=as.dendrogram(rowclust), > Colv=as.dendrogram(colclust), > scale='none', zlim=c(-clipper, clipper)) > > Karl Brand wrote: >> Esteemed BioC Users, >> >> In pursuit of a simple method to adjust the "contrast" of a heatmap i >> attempted to use the image() argument "zlim" in heatmap.2, both >> directly and using add.expr. These attempts failed (shown below on my >> example heatmap.2). >> >> heatplot() from library(made4) includes just such a convenient zlim= >> argument, but for now i'd trying to use heatmap.2 for all my heatmap >> needs. For the record, i can achieve what a zlim adjustment using the >> breaks argument to specify the effective range of zlim, but specifying >> breaks interferes with the scale argument in heatmap.2. So no good. >> >> So any suggestions for a zlim adjustment in heatmap.2 are greatly >> appreciated, cheers, >> >> Karl >> >> >> >> #example >> toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) >> library(gplots) >> windows() >> plot.new() >> heatmap.2(toy.mat, >> Rowv=T, Colv=T, >> dendrogram="column", >> scale= "row", >> col=c(greenred(256)), >> # breaks = c(seq(-10, 10, length.out = 257)),#interferes with 'scale' >> trace="none", >> density.info="none", >> zlim=c(-10, 10) #no error, but no effect >> # add.expr = (zlim=c(-10, 10)) #no error, but no effect >> # add.expr = "zlim", zlim=c(-10, 10) #no error, but no effect >> # add.expr = zlim, zlim=c(-10, 10) #produces error >> ) >> #end >> -- Karl Brand <k.brand at="" erasmusmc.nl=""> Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
ADD REPLY
0
Entering edit mode
Karl, You say "specifying breaks interferes with the scale argument in heatmap.2" but I get the exact same image from your heatmap.2 plot as when I plot an image of your scaled matrix "stdizedmat": a <- heatmap.2(toy.mat, Rowv=T, Colv=T, distfun=dist.pear, hclustfun=hclust.ave, scale= "row", col=greenred(256), breaks = c(seq(-2, 2, length.out = 257)), trace="none", density.info="none", main="heatmap.2 clustered" ) ##will produce heatmap image row.order<-a$rowInd col.order<-a$colInd windows() image(t(stdizedmat[row.order,col.order]),zlim=c(-clipper,clipper), col=greenred(256)) ##same image as before ...so I don't think it actually does mess up the scaling in this case. Elizabeth -----Original Message----- From: Karl Brand [mailto:k.brand@erasmusmc.nl] Sent: Friday, August 20, 2010 5:30 PM To: Kevin Coombes Cc: bioconductor at stat.math.ethz.ch; Elizabeth McClellan Subject: Re: [BioC] heatmap 'contrast': suggestions for a zlim adjustment in heatmap.2 Thank you Kevin, I took a punt on your suggestion which is very helpful given that i need more control over the details of my plots. But that also means i will need more understanding about what i'm doing and how to do it. Which i lack. I failed to duplicate the clustering i achieved within heatmap.2 when attempting to cluster 'manually'. The plots (run examples below) differ in row ordering. So far i cant work out how to eliminate this difference. Any help clarifying whats needed to manually duplicate the row ordering greatly appreciated, Karl ##run toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) #cluster manually then just plot with heatmap.2 rowclust <- hclust(as.dist(1-cor(t(toy.mat))), method="average") colclust <- hclust(as.dist(1-cor(toy.mat)), method="average") stdizedmat <- t(scale(t(toy.mat))) clipper <- 2 stdizedmat[stdizedmat < -clipper]<- -clipper stdizedmat[stdizedmat > clipper] <- clipper windows() plot.new() heatmap.2(stdizedmat, Rowv=as.dendrogram(rowclust), Colv=as.dendrogram(colclust), scale="none", zlim=c(-clipper, clipper), col=greenred(256), trace="none", density.info="none", main="manually clustered" ) #using heatmap.2 to cluster AND plot dist.pear <- function(x) as.dist(1-cor(t(x))) hclust.ave <- function(x) hclust(x, method="average") windows() plot.new() heatmap.2(toy.mat, Rowv=T, Colv=T, distfun=dist.pear, hclustfun=hclust.ave, scale= "row", col=greenred(256), breaks = c(seq(-2, 2, length.out = 257)), trace="none", density.info="none", main="heatmap.2 clustered" ) ##end On 8/19/2010 11:13 PM, Kevin Coombes wrote: > I've had the best results by treating heatmap as purely a display > function, and breaking out all of the processing in previous steps. To > make that more explicit, you can do something like > > rowclust <- hclust(mydDist(mat, "myMetric"), "myLinkage") > colclust <- hclust(myDist(mat, "anotherMetric"), "anotherLinkage") > stdizedmat <- t(scale(t(mat))) > clipper <- 3 > stdizedmat[stdizedmat < -clipper]<- -clipper > stdidezmat[stdizedmat > clipper] <- clipper > > heatmap(stdizedmat, > Rowv=as.dendrogram(rowclust), > Colv=as.dendrogram(colclust), > scale='none', zlim=c(-clipper, clipper)) > > Karl Brand wrote: >> Esteemed BioC Users, >> >> In pursuit of a simple method to adjust the "contrast" of a heatmap i >> attempted to use the image() argument "zlim" in heatmap.2, both >> directly and using add.expr. These attempts failed (shown below on my >> example heatmap.2). >> >> heatplot() from library(made4) includes just such a convenient zlim= >> argument, but for now i'd trying to use heatmap.2 for all my heatmap >> needs. For the record, i can achieve what a zlim adjustment using the >> breaks argument to specify the effective range of zlim, but specifying >> breaks interferes with the scale argument in heatmap.2. So no good. >> >> So any suggestions for a zlim adjustment in heatmap.2 are greatly >> appreciated, cheers, >> >> Karl >> >> >> >> #example >> toy.mat <- matrix(rnorm(1000), nrow=100, ncol=10) >> library(gplots) >> windows() >> plot.new() >> heatmap.2(toy.mat, >> Rowv=T, Colv=T, >> dendrogram="column", >> scale= "row", >> col=c(greenred(256)), >> # breaks = c(seq(-10, 10, length.out = 257)),#interferes with 'scale' >> trace="none", >> density.info="none", >> zlim=c(-10, 10) #no error, but no effect >> # add.expr = (zlim=c(-10, 10)) #no error, but no effect >> # add.expr = "zlim", zlim=c(-10, 10) #no error, but no effect >> # add.expr = zlim, zlim=c(-10, 10) #produces error >> ) >> #end >> -- Karl Brand <k.brand at="" erasmusmc.nl=""> Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
ADD REPLY
0
Entering edit mode
Hi Elizabeth, Certainly- your code yield's identical heatmap's. The motivation of my original post was how to conveniently achieve zlim adjustment within heatmap.2() ? la the zlim= arg in image() and heatplot(). I'd already achieved a 'hack' for this by specifying breaks, but this generated the warning- Warning message: In heatmap.2(toy.mat, Rowv = T, Colv = T, distfun = dist.pear, hclustfun = hclust.ave, : Using scale="row" or scale="column" when breaks arespecified can produce unpredictable results.Please consider using only one or the other. -which may or may not be a problem. At this point i hope not because it's the only way this R dilatant? can adjust the 'contrast' within heatmap.2. In response to Kevin Coombes suggestion to manually cluster then just plot with heatmap / heatmap.2: i was seeking an explanation why clustering within heatmap.2 yielded different row ordering compared to manually clustering despite having specified identical clustering functions for each approach (see code in my prevous email). I guess i was asking if some one could explain why this is so. Either i'm doing something wrong (if i expect identical heatmaps from the two approaches, or heatmap.2 works differently to the way i expect it would work- ie., cluster data based on specified functions, scale, plot. At this point, explicitly stating clustering then plotting these results (ie.,Kevin's suggestion) seems to me most appropriate when i want to be sure what i want is what i get. No less, your code certainly expands my understanding of how heatmap.2() and image() can be used which i really appreciate. That's a good thing- thank you. Further opinions and experience on the matter also appreciated, cheers, karl On 8/23/2010 1:27 PM, E. McClellan wrote: > Karl, > > You say "specifying breaks interferes with the scale argument in heatmap.2" > but > I get the exact same image from your heatmap.2 plot as when I plot an image > of > your scaled matrix "stdizedmat": > > a<- heatmap.2(toy.mat, > Rowv=T, Colv=T, > distfun=dist.pear, > hclustfun=hclust.ave, > scale= "row", > col=greenred(256), > breaks = c(seq(-2, 2, length.out = 257)), > trace="none", > density.info="none", > main="heatmap.2 clustered" > ) ##will produce heatmap image > > row.order<-a$rowInd > col.order<-a$colInd > > windows() > image(t(stdizedmat[row.order,col.order]),zlim=c(-clipper,clipper), > col=greenred(256)) ##same image as before > > ...so I don't think it actually does mess up the scaling in this case. > > Elizabeth > > > -----Original Message----- > From: Karl Brand [mailto:k.brand at erasmusmc.nl] > Sent: Friday, August 20, 2010 5:30 PM > To: Kevin Coombes > Cc: bioconductor at stat.math.ethz.ch; Elizabeth McClellan > Subject: Re: [BioC] heatmap 'contrast': suggestions for a zlim adjustment in > heatmap.2 > > Thank you Kevin, > > I took a punt on your suggestion which is very helpful given that i need > more control over the details of my plots. But that also means i will > need more understanding about what i'm doing and how to do it. Which i lack. > > I failed to duplicate the clustering i achieved within heatmap.2 when > attempting to cluster 'manually'. The plots (run examples below) differ > in row ordering. So far i cant work out how to eliminate this difference. > > Any help clarifying whats needed to manually duplicate the row ordering > greatly appreciated, > > Karl > > > ##run > toy.mat<- matrix(rnorm(1000), nrow=100, ncol=10) > #cluster manually then just plot with heatmap.2 > rowclust<- hclust(as.dist(1-cor(t(toy.mat))), method="average") > colclust<- hclust(as.dist(1-cor(toy.mat)), method="average") > stdizedmat<- t(scale(t(toy.mat))) > clipper<- 2 > stdizedmat[stdizedmat< -clipper]<- -clipper > stdizedmat[stdizedmat> clipper]<- clipper > windows() > plot.new() > heatmap.2(stdizedmat, > Rowv=as.dendrogram(rowclust), > Colv=as.dendrogram(colclust), > scale="none", zlim=c(-clipper, clipper), > col=greenred(256), > trace="none", > density.info="none", > main="manually clustered" > ) > > #using heatmap.2 to cluster AND plot > dist.pear<- function(x) as.dist(1-cor(t(x))) > hclust.ave<- function(x) hclust(x, method="average") > windows() > plot.new() > heatmap.2(toy.mat, > Rowv=T, Colv=T, > distfun=dist.pear, > hclustfun=hclust.ave, > scale= "row", > col=greenred(256), > breaks = c(seq(-2, 2, length.out = 257)), > trace="none", > density.info="none", > main="heatmap.2 clustered" > ) > ##end > > On 8/19/2010 11:13 PM, Kevin Coombes wrote: >> I've had the best results by treating heatmap as purely a display >> function, and breaking out all of the processing in previous steps. To >> make that more explicit, you can do something like >> >> rowclust<- hclust(mydDist(mat, "myMetric"), "myLinkage") >> colclust<- hclust(myDist(mat, "anotherMetric"), "anotherLinkage") >> stdizedmat<- t(scale(t(mat))) >> clipper<- 3 >> stdizedmat[stdizedmat< -clipper]<- -clipper >> stdidezmat[stdizedmat> clipper]<- clipper >> >> heatmap(stdizedmat, >> Rowv=as.dendrogram(rowclust), >> Colv=as.dendrogram(colclust), >> scale='none', zlim=c(-clipper, clipper)) >> >> Karl Brand wrote: >>> Esteemed BioC Users, >>> >>> In pursuit of a simple method to adjust the "contrast" of a heatmap i >>> attempted to use the image() argument "zlim" in heatmap.2, both >>> directly and using add.expr. These attempts failed (shown below on my >>> example heatmap.2). >>> >>> heatplot() from library(made4) includes just such a convenient zlim= >>> argument, but for now i'd trying to use heatmap.2 for all my heatmap >>> needs. For the record, i can achieve what a zlim adjustment using the >>> breaks argument to specify the effective range of zlim, but specifying >>> breaks interferes with the scale argument in heatmap.2. So no good. >>> >>> So any suggestions for a zlim adjustment in heatmap.2 are greatly >>> appreciated, cheers, >>> >>> Karl >>> >>> >>> >>> #example >>> toy.mat<- matrix(rnorm(1000), nrow=100, ncol=10) >>> library(gplots) >>> windows() >>> plot.new() >>> heatmap.2(toy.mat, >>> Rowv=T, Colv=T, >>> dendrogram="column", >>> scale= "row", >>> col=c(greenred(256)), >>> # breaks = c(seq(-10, 10, length.out = 257)),#interferes with 'scale' >>> trace="none", >>> density.info="none", >>> zlim=c(-10, 10) #no error, but no effect >>> # add.expr = (zlim=c(-10, 10)) #no error, but no effect >>> # add.expr = "zlim", zlim=c(-10, 10) #no error, but no effect >>> # add.expr = zlim, zlim=c(-10, 10) #produces error >>> ) >>> #end >>> > -- Karl Brand <k.brand at="" erasmusmc.nl=""> Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
ADD REPLY

Login before adding your answer.

Traffic: 526 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6