Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • crazyhottommy
    Senior Member
    • Apr 2012
    • 187

    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

    Thanks.
  • jmw86069
    Member
    • Jun 2009
    • 31

    #2
    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.

    Comment

    • crazyhottommy
      Senior Member
      • Apr 2012
      • 187

      #3
      Originally posted by jmw86069 View Post
      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?
      Last edited by crazyhottommy; 08-23-2013, 04:16 AM.

      Comment

      • jmw86069
        Member
        • Jun 2009
        • 31

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

        Code:
        bk <- [COLOR="DarkRed"]unique[/COLOR](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.

        Comment

        • jmw86069
          Member
          • Jun 2009
          • 31

          #5
          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...

          Comment

          • crazyhottommy
            Senior Member
            • Apr 2012
            • 187

            #6
            Originally posted by jmw86069 View Post
            Well the "breaks are not unique" part is straightforward, just try this:

            Code:
            bk <- [COLOR="DarkRed"]unique[/COLOR](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.





            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!

            Comment

            • crazyhottommy
              Senior Member
              • Apr 2012
              • 187

              #7
              Originally posted by crazyhottommy View Post
              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.





              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...

              Comment

              • crazyhottommy
                Senior Member
                • Apr 2012
                • 187

                #8
                Originally posted by crazyhottommy View Post
                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...

                Comment

                • jmw86069
                  Member
                  • Jun 2009
                  • 31

                  #9
                  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();
                  Attached Files

                  Comment

                  • crazyhottommy
                    Senior Member
                    • Apr 2012
                    • 187

                    #10
                    Originally posted by jmw86069 View Post
                    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.

                    Comment

                    • jp.
                      Senior Member
                      • Jul 2013
                      • 142

                      #11
                      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.

                      Comment

                      Latest Articles

                      Collapse

                      • SEQadmin2
                        Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                        by SEQadmin2



                        Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                        ...
                        Yesterday, 11:10 AM
                      • SEQadmin2
                        Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                        by SEQadmin2



                        Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                        There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                        07-08-2026, 05:17 AM
                      • GATTACAT
                        Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                        by GATTACAT
                        Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                        07-01-2026, 11:43 AM

                      ad_right_rmr

                      Collapse

                      News

                      Collapse

                      Topics Statistics Last Post
                      Started by SEQadmin2, Yesterday, 10:04 AM
                      0 responses
                      9 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-08-2026, 10:08 AM
                      0 responses
                      7 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-07-2026, 11:05 AM
                      0 responses
                      12 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-02-2026, 11:08 AM
                      0 responses
                      31 views
                      0 reactions
                      Last Post SEQadmin2  
                      Working...