'R heatmap function - can you add more colours so that differences within large regions are more obvious?

I am plotting a heatmap in R using the base R heatmap() function. Is there a way to define more colours so that the heatmap has a greater variation in the colours used. Currently it is using about 10 and the "hottest" area is quite large and dark purple. I want more colours so that this large area itself it broken down into more colours to better differentiate.



Solution 1:[1]

Try experimenting with the color palettes of the grDevices package.

     library(grDevices)
     heatmap(x, col = topo.colors(n))

where n is the number of colors. Or, alternatively

     col = rainbow(n)
     col = terrain.colors(n)
     col = cm.colors(n)

However, often the problem with differentiation does not depend on the number of colors, but on the data variability: many of them may be clustered in a small range of values. In such case you could try to differentiate them by chosing a subrange or transforming the data, for example by graphing their logaritm.

Examples:

  1. 50 colors from cm.colors palette:

    heatmap(Ca, col=cm.colors(50), Rowv=NA, Colv=NA)

enter image description here

  1. matrix of log values, with 50 colors from cm.colors palette:

    heatmap(log(Ca), col=cm.colors(50), Rowv=NA, Colv=NA)

enter image description here

in which subtler differences can be seen.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1