Customize range heatmap.2

jensjorda picture jensjorda · May 4, 2014 · Viewed 11.5k times · Source

I am using the heatmap.2 function from the "gplots" package and would like to tweak the vizual output.

I would like to get a symmetric color scheme and I have achieved that using the scale="row" option. However, when doing this, my range gets narrowed down to a small interval c(-1 , 1). The range of my data is c(-1.2 , 2.97), and I would like the color scheme to capture the full resolution of my data. Can I specify the colour range to be e.g. c(-3,3) or is there another way to get a higher resolution of my data

This is my code

>head(hmdat[select,])
          Ind_B    Ind_Z     Ind_P
SVS1 -0.2176648 1.540470 -0.03700749
RHR2  0.3814377 1.425968  0.45827347
ADR1  0.7410964 1.062387  1.37050918
SPI1  0.2722683 1.324529  0.72636276
YAR1  0.7309389 1.278198  0.98776034
DDR2 -0.1727803 2.470086  0.74259052

>hmcol = colorRampPalette(brewer.pal(9, "RdBu"))(100)
>heatmap.2(hmdat[select,],scale="row", col = hmcol, trace="none", margin=c(7, 5),cexCol=0.9,cexRow=0.5,density.info="density")

generating this plot

plot

Another thing that I would like to fix is to remove the title of colour key and being able to specify manually the axis labels of the colour key. Thanks

Answer

ping picture ping · May 5, 2014

Using scale = "row" will scale your data by row and doesn't keep your original, raw values so you should use scale = "none" if you want to preserve those.

You can set breaks manually by using the breaks argument. In this case, I think you could achieve what you're looking for by creating a sequence from -3 to 3, and setting length.out to one more than the number of colours you've defined (there needs to be 1 more breaks than colours):

heatmap.2(hmdat[select,],scale="none", col = hmcol, trace="none", margin=c(7, 5),cexCol=0.9,cexRow=0.5,density.info="density",breaks = seq(-3, 3, length.out = 101))

You might get warnings about there being no data for some values, and you may need to add symbreaks = TRUE to get the symmetrical effect you're looking for.

I don't think there is a simple option to change the labels of the color key, but you can try the solution in this question: How to change the color key value in heatmap.2?