Custom colormap for image() in R

Creating a custom colormap in R to plot a matrix is simple:

nsamples <- 20
matrix2plot <- 1:nsamples
dim(matrix2plot) <- c(4, 5)
colors2spaceThrough <- c('red', 'white', 'blue')
customColorMap <- colorRampPalette(colors2spaceThrough)(nsamples)
image(1:4, 1:5, matrix2plot, col = customColorMap)

However, in spite all my efforts, I struggled quite a bit with finding an example like the one above on the web. My goal was to display a matrix using a color map suitable for colorblind people (i.e., using red, white and blue). Also, the color map should have had a range easily extensible, and it should not have required more than five lines of code. Sadly, I spent more than a few hours searching and tweaking others people code before coming to a solution. Trying to decipher other people’s code I find especially disappointing, particularly when the code seem too complex or elaborated for solving such a simple problem. I even found functions that supposedly create a customized color map, but why? Don’t you guys think that if that would be necessary the R-core team would have wrote such a function? … a function for a color map.. what an overkill! And then what? Am I supposed to source the code with the colormap function every time I want to plot a matrix?

Anyway, I think my inability of coming up with the above code was mostly due to my dominantly `matlabian’ framework. When I started thinking about this issue I had already solved it in matlab. However since a student needed an R alternative I attempted a translation matlab R (why does google translate not have this option?):

start = [55, 96, 146]/255;
middle = ones(1, 3);
endC = [238, 43, 39]/255;
nsteps = 20;
customMap = [linspace(start(1), middle(1), floor(nsteps/2)), ...
linspace(middle(1), endC(1), ceil(nsteps/2)); ...
linspace(start(2), middle(2), floor(nsteps/2)), ...
linspace(middle(2), endC(2), ceil(nsteps/2)); ...
linspace(start(3), middle(3), floor(nsteps/2)), ...
linspace(middle(3), endC(3), ceil(nsteps/2))]';
customMap(round(nsteps/2), : ) = []; % remove redundant white color
image2plot = randi(4, 5);

Since I regard matlab and R quite similar, at least in the logical indexing kind of way, I thought translating matlab to R would have been a piece of cake. My first attempt was to create a custom matrix of RGB values to use for my color map. I thought I could have just plug that as an index into a sequence of colors to pass to `col’ within the plot call in R.  Needless to say that was not the right approach. Then I started searching what other people where doing to plot a matrix. I found many responses and approaches. However, understanding other people’s approach (and code) required sometimes so much effort that I could not believe it was so difficult to just plot a color coded matrix. But digging and digging I finally found the colorRampPalette function which, after a bit of exploration, does exactly what I needed. And let me say that I think it does it a little bit more elegantly than matlab.

p.s.: after a bit of scrolling down on a page I left open during my google search, I found the exact solution to my quest in this answer hidden in the bottom of a stackoverflow answer. This proves that people do not finish reading articles, even when potentially interesting solutions are at the bottom. This poses an interesting problem since it seems that the stackoverflow page returned by google contained the solution for my problem after all. However, it was buried at the bottom of the page. But how could the right answer emerge? It seems an endless chain of frustrations: to the frustration of matlabing in R followed the joy of solving the problem, followed by the frustration that the answer might have been looking at me all the time… I just wasn’t looking right! I should go get a prescription for glasses.

Custom colormap for image() in R

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s