3

I am trying to make a heatmap in R where my data ranges from -5 to +50. I would like values of 0 to be white, the values between 0 and -5 to be coloured on a gradient from white to blue and the values between 0 and +50 to be coloured from white to red. Ideally, I'd also like the same scaling to be used, i.e. the darkness of blue at -5 to be the same as the darkness of the red at +5.

I have working code for heatmap with the gradient for the whole data set from blue to red with white colouring points close to the mean of the data.

hv <- heatmap.2(data_matrix,
    scale="none",
    Rowv=NA, 
    Colv=NA, 
    col = rev(brewer.pal(11,"RdBu")), 
    margins=c(5,5), 
    cexRow=0.5, cexCol=1.0,
    ylab= "Mutations",
    main = "heatmap", 
    key=TRUE,keysize=1.5, trace="none")

Any suggestions would be greatly appreciated.

Cheers!

sat_s
  • 277
  • 1
  • 5
  • 11
  • 1
    Have you taken a look at this recent and very similar question: http://stackoverflow.com/questions/8161014/custom-heat-map-in-r/8161231#8161231 – Josh O'Brien Nov 21 '11 at 20:29
  • Do you mean instead that you want "the darkness of blue at -5 to be the same as the darkness of **the red at +50**"? Finally, could you include a small submatrix called `data_matrix` (and calls to `1ibrary(RColorBrewer)` and `library(gplots)`) to make this a reproducible example? – Josh O'Brien Nov 21 '11 at 21:18

2 Answers2

2

You need to add these to you function.

breaks = c(5/6* -5,5/6* -4,5/6* -3,5/6* -2,5/6* -1,0,5/6* 1,50/6*1,50/6*2,50/6*3,50/6*4,50/6*5)

breaks=breaks

This specifies the breaks you use in the heatmap. You might try to fudge the numbers a bit, and mayby add a few more colors and breaks, the number of breaks should be number of colors + 1.

Giving this:

library(RColorBrewer) 
library(gplots)
data_matrix <- cbind(c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)),
            c(rnorm(30,-2.5,sd= 0.85),rnorm(30,25,sd= 8),rnorm(30,6,sd= 3)))

breaks = c(5/6* -5, 5/6* -4, 5/6* -3, 5/6* -2, 5/6* -1, 0 ,5/6* 1, 50/6*1, 50/6*2, 50/6*3, 50/6*4, 50/6*5)
hv <- heatmap.2(data_matrix, 
    scale="none",
    Rowv=NA,
    Colv=NA,
    col = rev(brewer.pal(11,"RdBu")),
    margins=c(5,5),
    cexRow=0.5, cexCol=1.0, 
    breaks=breaks,
    ylab= "Mutations",
    main = "heatmap",
    key=TRUE,keysize=1.5, trace="none")
Mischa Vreeburg
  • 1,576
  • 1
  • 13
  • 18
2

If your data goes from -5 to +50, having the same gradient for positive and negative values will make the negative colours look pretty much all white,

washedout

In contrast, the full hue version goes like this,

fullhue

baptiste
  • 75,767
  • 19
  • 198
  • 294