Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

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

  • #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


    • #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


      • #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


        • #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


          • #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


            • #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


              • #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


                • #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


                  • #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


                    • #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

                      • seqadmin
                        Strategies for Sequencing Challenging Samples
                        by seqadmin


                        Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
                        03-22-2024, 06:39 AM
                      • seqadmin
                        Techniques and Challenges in Conservation Genomics
                        by seqadmin



                        The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

                        Avian Conservation
                        Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
                        03-08-2024, 10:41 AM

                      ad_right_rmr

                      Collapse

                      News

                      Collapse

                      Topics Statistics Last Post
                      Started by seqadmin, Yesterday, 06:37 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post seqadmin  
                      Started by seqadmin, Yesterday, 06:07 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post seqadmin  
                      Started by seqadmin, 03-22-2024, 10:03 AM
                      0 responses
                      49 views
                      0 likes
                      Last Post seqadmin  
                      Started by seqadmin, 03-21-2024, 07:32 AM
                      0 responses
                      67 views
                      0 likes
                      Last Post seqadmin  
                      Working...
                      X