SEQanswers

SEQanswers (http://seqanswers.com/forums/index.php)
-   Bioinformatics (http://seqanswers.com/forums/forumdisplay.php?f=18)
-   -   map colour to values in heatmap (http://seqanswers.com/forums/showthread.php?t=33023)

crazyhottommy 08-22-2013 01:00 PM

map colour to values in heatmap
 
Hi all,

I am plotting a matrix to heatmap by heatmap.2.
my matrix data contain many 0s, and some other value range from 0 to 10
I want all the value below 0 and including 0 represent by white, positive values be represent gradient from white to red.

Any ideas how to do it:confused:

Thanks.

jmw86069 08-23-2013 03:20 AM

Use the 'breaks' parameter of the heatmap.2() function. Set it to a vector of values representing where the color breaks should be. I think you need n+1 breaks for colors, but in this rare case the error message will guide you. Just be sure to use a color vector one fewer length than the breaks. Not too hard once you get it working, and it's much more powerful that way.

crazyhottommy 08-23-2013 04:12 AM

Quote:

Originally Posted by jmw86069 (Post 114267)
Use the 'breaks' parameter of the heatmap.2() function. Set it to a vector of values representing where the color breaks should be. I think you need n+1 breaks for colors, but in this rare case the error message will guide you. Just be sure to use a color vector one fewer length than the breaks. Not too hard once you get it working, and it's much more powerful that way.

my code:
bk = c(seq(-0.1,0, length=100), seq(0,0.4,length=100),seq(0.4,13,length=100))
hmcols<- colorRampPalette(c("white","red","red4"))(length(bk)-1)

heatmap.2(m1, col=hmcols, breaks = bk, Colv=FALSE, dendrogram="row", symkey=FALSE, symm=F, symbreaks=T, scale="none", trace="none", labRow=NA, labCol=NA)


Error in heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
lazy-load database 'P' is corrupt
In addition: Warning messages:
1: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
restarting interrupted promise evaluation
2: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
internal error -3 in R_decompress1ols<- colorRampPalette(c("white","red","red4"))(length(bk)-1)
-------------------------------------------------------------------------------------------------
if I use pheatmap to plot

pheatmap(m1, color=hmcols, breaks= bk, cluster_rows=TRUE, cluster_cols=FALSE, legend=FALSE, show_rownames=FALSE, show_colnames=FALSE)
Error in cut.default(x, breaks = breaks, include.lowest = T) :
'breaks' are not unique

---------------------------------------------------------------
it complains breaks are not unique...
what's wrong with my code?

jmw86069 08-23-2013 04:26 AM

Well the "breaks are not unique" part is straightforward, just try this:

Code:

bk <- unique(c(seq(-0.1,0, length=100), seq(0,0.4,length=100),seq(0.4,13,length=100)));
It is caused by concatenating several seq() results together, which share the same boundaries.

I'm testing out your code now, just have to create a matrix m1 to make it reproducible before I post back with an example that works.

jmw86069 08-23-2013 05:07 AM

Okay, a few things going on. First, do you want the range from 0 to 0.4 to be solid white? Right now, your code will set white to ~0.2, since that is the midpoint between 0 and 0.4.

If you want fine-grained control over the color ramps, it may be easier to make 3 color ramps and concatenate them together. So if you want 0 to 0.4 to be white, you can set that ramp all white, for example.

A quick way to test how the colors will get applied is to cut() the m1 matrix using the breaks defined by bk:

Code:

m1cut <- cut(m1, bk);
Then look at the levels of m1cut, which will tell you the ranges for which that the color ramp will be applied:

Code:

levels(m1cut)
However, I'd suggest using far fewer than 100 breaks, especially for testing! Try something like 5.

For breaks to work, you only need to specify the boundaries where you want your data to be cut(), and then specify a color ramp in sync with those boundary positions. So if you want 0 to 0.4 to be white, specify boundaries at 0, and 0.4, and make sure your color ramp has "white" at that position. I don't know if any of this makes sense without R code...

crazyhottommy 08-23-2013 05:36 AM

Quote:

Originally Posted by jmw86069 (Post 114270)
Well the "breaks are not unique" part is straightforward, just try this:

Code:

bk <- unique(c(seq(-0.1,0, length=100), seq(0,0.4,length=100),seq(0.4,13,length=100)));
It is caused by concatenating several seq() results together, which share the same boundaries.

I'm testing out your code now, just have to create a matrix m1 to make it reproducible before I post back with an example that works.


Thanks! it worked with your method. the reason I want to map the value to colour is that I want to compare different heatmaps just by the colour.

I have two matrix, but the data range is different
one is from -0.1 to 9
the other is from -0.1 to 12

I think I just have to play around with the colour scheme to make my heatmap look better.

http://s21.postimg.org/72rw342tj/image.png
http://s10.postimg.org/geed1tdrd/image.png


It is from ChIP-seq data. I plot the tag intensity around TSS.

code:
bk = unique(c(seq(-0.1,0.5, length=100),seq(0.5,13,length=100)))
hmcols<- colorRampPalette(c("white","red4"))(length(bk)-1)

the previous code gave me a lot "red" background which I do not want.

Thank you again!

crazyhottommy 08-23-2013 07:32 AM

Quote:

Originally Posted by crazyhottommy (Post 114275)
Thanks! it worked with your method. the reason I want to map the value to colour is that I want to compare different heatmaps just by the colour.

I have two matrix, but the data range is different
one is from -0.1 to 9
the other is from -0.1 to 12

I think I just have to play around with the colour scheme to make my heatmap look better.

http://s21.postimg.org/72rw342tj/image.png
http://s10.postimg.org/geed1tdrd/image.png


It is from ChIP-seq data. I plot the tag intensity around TSS.

code:
bk = unique(c(seq(-0.1,0.5, length=100),seq(0.5,13,length=100)))
hmcols<- colorRampPalette(c("white","red4"))(length(bk)-1)

the previous code gave me a lot "red" background which I do not want.

Thank you again!

The same code worked for pheatmap but still gave me error if I use heatmap.2
heatmap.2(m1, col=hmcols, breaks = bk, Colv=FALSE, dendrogram="row", symkey=FALSE, symm=F, symbreaks=T, scale="none", trace="none", labRow=NA, labCol=NA)
Error in heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
lazy-load database 'P' is corrupt
In addition: Warning messages:
1: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
restarting interrupted promise evaluation
2: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
internal error -3 in R_decompress1

why is that? the error message is kind of mysterious...

crazyhottommy 08-23-2013 01:35 PM

Quote:

Originally Posted by crazyhottommy (Post 114284)
The same code worked for pheatmap but still gave me error if I use heatmap.2
heatmap.2(m1, col=hmcols, breaks = bk, Colv=FALSE, dendrogram="row", symkey=FALSE, symm=F, symbreaks=T, scale="none", trace="none", labRow=NA, labCol=NA)
Error in heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
lazy-load database 'P' is corrupt
In addition: Warning messages:
1: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
restarting interrupted promise evaluation
2: In heatmap.2(m1, col = hmcols, breaks = bk, Colv = FALSE, dendrogram = "row", :
internal error -3 in R_decompress1

why is that? the error message is kind of mysterious...

I think it is just my R setting problem, though I do not know exactly what's the problem.
I tried the same code with heatmap.2 in the HPC computing cluster, it worked fine.

Sorry to bother you...

jmw86069 08-23-2013 08:44 PM

1 Attachment(s)
No bother at all, sorry I didn't get back to this earlier in the day. The "lazy-load database 'P' is corrupt" sounds like a symptom of memory limits, or session corruption, or both. Occasionally I'll have an R session go stale -- either the tempdir() cannot be found or the X-windows lost its authentication. In either case the most time-effective fix is to save the R session (and the history), close and re-open R. So did these errors all come from one R session, or are they reproducible each time you open a new R session? I'd check that you're indeed running 64-bit R on whatever platform. See if it works fine with only half the rows of data.

Now for my heatmap power tip of the day, if you will: use the "useRaster=TRUE" parameter in your heatmap.2() call. Excellent extension by R developers since 2.13. But for some reason the R developers explicitly turn it off for interactive session windows, so you'll only see it in an exported file. (Unless you have a custom image() function which doesn't disable it, I did this.) It also makes the exported file hugely smaller, especially for PDFs.

What it does is darn-near essential for nextgen coverage heatmaps -- it actually properly resamples the image as it down-sizes the image during export. Without useRaster=TRUE, image() creates a zillion tiny rectangles to represent the heatmap, all pieced together right next to each other. When the display is fewer pixels/points high than the number of rows of data, it discretizes the data -- that is, it uses integer values for the rectangles. In many cases, especially onscreen, many rectangles fully overlap others, randomly obscuring the real patterns, and often blunting your otherwise cool-looking signal.

Best way to test is export to PDF with useRaster=FALSE, then do it again with useRaster=TRUE. For me, night and day.
Code:

library(gplots);
## Prep some random noise sample data
testHeatdata <- matrix(rnorm(90000), nrow=10000, ncol=9);
testHeatdata <- testHeatdata[order(testHeatdata[,5]),];
testHeatdata2 <- testHeatdata;
g1 <- seq(from=10, to=10000, by=1000);
## Make some tiny horizontal stripes
testHeatdata2[g1+rep(1:3, each=length(g1)),] <- 9;

## Try to hide a little happy face
testHeatdata3 <- testHeatdata;
for (i in seq(from=125, to=235, by=3)) {
  ix <- round(sin(deg2rad(i))*5+5);
  iy <- round(-cos(deg2rad(i))*5500 + 3500);
  testHeatdata3[iy:(iy+4), ix] <- 10;
}
g2 <- 3011+c(1:12*90);
testHeatdata3[g2+rep(1:3, each=length(g2)), 4] <- 10;
testHeatdata3[g2+rep(1:3, each=length(g2)), 7] <- 10;

cairo_pdf("testHeatmapRaster.pdf", onefile=TRUE);
heatmap.2(testHeatdata2, Rowv=FALSE, Colv=FALSE, useRaster=FALSE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=FALSE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata2, Rowv=FALSE, Colv=FALSE, useRaster=TRUE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=TRUE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata3, Rowv=FALSE, Colv=FALSE, useRaster=FALSE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=FALSE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata3, Rowv=FALSE, Colv=FALSE, useRaster=TRUE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=TRUE", col="redblue", trace="none", key=FALSE);
dev.off();


crazyhottommy 08-24-2013 12:20 PM

Quote:

Originally Posted by jmw86069 (Post 114323)
No bother at all, sorry I didn't get back to this earlier in the day. The "lazy-load database 'P' is corrupt" sounds like a symptom of memory limits, or session corruption, or both. Occasionally I'll have an R session go stale -- either the tempdir() cannot be found or the X-windows lost its authentication. In either case the most time-effective fix is to save the R session (and the history), close and re-open R. So did these errors all come from one R session, or are they reproducible each time you open a new R session? I'd check that you're indeed running 64-bit R on whatever platform. See if it works fine with only half the rows of data.

Now for my heatmap power tip of the day, if you will: use the "useRaster=TRUE" parameter in your heatmap.2() call. Excellent extension by R developers since 2.13. But for some reason the R developers explicitly turn it off for interactive session windows, so you'll only see it in an exported file. (Unless you have a custom image() function which doesn't disable it, I did this.) It also makes the exported file hugely smaller, especially for PDFs.

What it does is darn-near essential for nextgen coverage heatmaps -- it actually properly resamples the image as it down-sizes the image during export. Without useRaster=TRUE, image() creates a zillion tiny rectangles to represent the heatmap, all pieced together right next to each other. When the display is fewer pixels/points high than the number of rows of data, it discretizes the data -- that is, it uses integer values for the rectangles. In many cases, especially onscreen, many rectangles fully overlap others, randomly obscuring the real patterns, and often blunting your otherwise cool-looking signal.

Best way to test is export to PDF with useRaster=FALSE, then do it again with useRaster=TRUE. For me, night and day.
Code:

library(gplots);
## Prep some random noise sample data
testHeatdata <- matrix(rnorm(90000), nrow=10000, ncol=9);
testHeatdata <- testHeatdata[order(testHeatdata[,5]),];
testHeatdata2 <- testHeatdata;
g1 <- seq(from=10, to=10000, by=1000);
## Make some tiny horizontal stripes
testHeatdata2[g1+rep(1:3, each=length(g1)),] <- 9;

## Try to hide a little happy face
testHeatdata3 <- testHeatdata;
for (i in seq(from=125, to=235, by=3)) {
  ix <- round(sin(deg2rad(i))*5+5);
  iy <- round(-cos(deg2rad(i))*5500 + 3500);
  testHeatdata3[iy:(iy+4), ix] <- 10;
}
g2 <- 3011+c(1:12*90);
testHeatdata3[g2+rep(1:3, each=length(g2)), 4] <- 10;
testHeatdata3[g2+rep(1:3, each=length(g2)), 7] <- 10;

cairo_pdf("testHeatmapRaster.pdf", onefile=TRUE);
heatmap.2(testHeatdata2, Rowv=FALSE, Colv=FALSE, useRaster=FALSE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=FALSE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata2, Rowv=FALSE, Colv=FALSE, useRaster=TRUE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=TRUE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata3, Rowv=FALSE, Colv=FALSE, useRaster=FALSE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=FALSE", col="redblue", trace="none", key=FALSE);
heatmap.2(testHeatdata3, Rowv=FALSE, Colv=FALSE, useRaster=TRUE, labRow=NA, symbreaks=TRUE, breaks=51,
  main="useRaster=TRUE", col="redblue", trace="none", key=FALSE);
dev.off();



Thank you so much for your insight! I am running 64-bit R in ubuntu 12.04 through Emacs.
I restarted emacs and run the code, it worked fine!

Also, I am so happy to know the useRaster argument. I learned a lot from you.
:)

jp. 04-09-2014 03:30 AM

hi all
Can somebody expain me to add five different colors in heatmap.2 (gplots). for example:
I have nine different values (-1 -0.8 -0.5 -0.2 0 0.2 0.5 0.8 1) and I want nine colors of my choice (blue, red, green, pin, white, black, brown, grey, orange); and my values are:
details sample1 sample2 sample3 sample4 sample5
ab -1 1 0.8 0.5 0.2
abc -0.8 -1 1 0.8 0.5
bc -0.5 -0.8 -1 1 0.8
cd -0.2 -0.5 -0.8 -1 1
cde 0 -0.2 -0.5 -0.8 -1
de 0.2 0 -0.2 -0.5 -0.8
def 0.5 0.2 0 -0.2 -0.5
ef 0.8 0.5 0.2 0 -0.2
efg 1 0.8 0.5 0.2 0


Can somebody expert tell me how can I do this?
thank you
jp.


All times are GMT -8. The time now is 07:59 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2021, vBulletin Solutions, Inc.