So much to do, so little time

Trying to squeeze sense out of chemical data

A Custom Palette for Heatmaps

with 2 comments

Heatmaps are a common way to visualize matrices and R provides a variety of methods to generate these diagrams. One of the key features of a heatmap is the color scheme employed. By default the image method uses heat.colors which ranges from red (lowest values) to white (highest values). Other palettes include rainbow and topographical. However, I needed to replicate a color scheme used in a previous publication – dark red for low values and white for high values, passing through shades of pink. While RColorBrewer is an extremely useful package for generating sensible and informative color schemes it didn’t help me out here – the red palette was limited to 9 colors, whereas I needed a larger range. There’s also the colorschemes method from the ClassDiscovery package, but I haven’t checked that out.

So, I ended up making my own color scheme, realizing that shades of pink are a constant red, coupled with increasing amounts of blue and green (in the RGB scheme). Thus generating a matrix of R, G and B values, with a constant red value and increasing blue and green values and then converting the rows to hex colors (via rgb, gives me the required colors.

rgbs <- cbind(255,
                     seq(32,255, length=256),
                     seq(32,255, length=256)) / 255
cols <- rgb(rgbs[,1], rgbs[,2], rgbs[,3])

and an example of the color scheme

image(matrix(1:256, nrow=64, byrow=TRUE),

resulting in

Written by Rajarshi Guha

October 28th, 2009 at 1:26 pm

Posted in Uncategorized,research

Tagged with , ,

2 Responses to 'A Custom Palette for Heatmaps'

Subscribe to comments with RSS or TrackBack to 'A Custom Palette for Heatmaps'.

  1. when I was looking at colour maps, I found this to be a great help

    Richard Hall

    2 Nov 09 at 5:00 pm

  2. Richard, thanks for the pointer. This is a very handy resource

    Rajarshi Guha

    2 Nov 09 at 10:53 pm

Leave a Reply