How to change heatmap.2 color range in R?

前端 未结 4 944
小蘑菇
小蘑菇 2020-12-23 12:21

I\'m using gplot to produce a heatmap showing log2-fold changes of a treatment groups versus paired controls. With the following code:

 heatmap.2(as.matrix(S         


        
相关标签:
4条回答
  • 2020-12-23 13:05

    I think you need to set symbreaks = FALSE That should allow for asymmetrical color scales.

    0 讨论(0)
  • 2020-12-23 13:10

    You could try to create your own color palette using the RColorBrewer package

    my_palette <- colorRampPalette(c("green", "black", "red"))(n = 1000)
    

    and see how this looks like. But I assume in your case only scaling would help if you really want to keep the black in "the middle". You can simply use my_palette instead of the redgreen()

    I recommend that you check out the RColorBrewer package, they have pretty nice in-built palettes, and see interactive website for colorbrewer.

    0 讨论(0)
  • 2020-12-23 13:12

    I got the color range to be asymmetric simply by changing the symkey argument to FALSE

    symm=F,symkey=F,symbreaks=T, scale="none"
    

    Solved the color issue with colorRampPalette with the breaks argument to specify the range of each color, e.g.

    colors = c(seq(-3,-2,length=100),seq(-2,0.5,length=100),seq(0.5,6,length=100))
    
    my_palette <- colorRampPalette(c("red", "black", "green"))(n = 299)
    

    Altogether

    heatmap.2(as.matrix(SeqCountTable), col=my_palette, 
        breaks=colors, density.info="none", trace="none", 
            dendrogram=c("row"), symm=F,symkey=F,symbreaks=T, scale="none")
    
    0 讨论(0)
  • 2020-12-23 13:27

    Here's another option for those not using heatmap.2 (aheatmap is good!)

    Make a sequential vector of 100 values from min to max of your input matrix, find value closest to 0 in that, make two vector of colours to and from desired midpoint, combine and use them:

    breaks <- seq(from=min(range(inputMatrix)), to=max(range(inputMatrix)), length.out=100)
    midpoint <- which.min(abs(breaks - 0))
    rampCol1 <- colorRampPalette(c("forestgreen", "darkgreen", "black"))(midpoint)
    rampCol2 <- colorRampPalette(c("black", "darkred", "red"))(100-(midpoint+1))
    rampCols <- c(rampCol1,rampCol2)
    
    0 讨论(0)
提交回复
热议问题