Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: heatmap.2 change color key

Tags:

r

colors

heatmap

I have a question about the package gplots. I want to use the function heatmap.2 and therefore I want to change my symmetric point in color key from 0 to 1. Normally when symkey=TRUE and you use the col=redgreen(), a colorbar is created where the colors are managed like this:

red = -2 to -0.5
black=-0.5 to 0.5
green= 0.5 to 2

Now i want to create a colorbar like this:

red= -1 to 0.8
black= 0.8 to 1.2
green= 1.2 to 3

Is something like this possible?

Thank you!

like image 437
Lisann Avatar asked Dec 21 '11 10:12

Lisann


1 Answers

If you look at the heatmap.2 help file, it looks like you want the breaks argument. From the help file:

breaks (optional) Either a numeric vector indicating the splitting points for binning x into colors, or a integer number of break points to be used, in which case the break points will be spaced equally between min(x) and max(x)

So, you use breaks to specify the cutoff points for each colour. e.g.:

library(gplots)

# make up a bunch of random data from -1, -.9, -.8, ..., 2.9, 3
# 10x10
x = matrix(sample(seq(-1,3,by=.1),100,replace=TRUE),ncol=10)

# plot. We want -1 to 0.8 being red, 0.8 to 1.2 being black, 1.2 to 3 being green.
heatmap.2(x, col=redgreen, breaks=c(-1,0.8,1.2,3))

The crucial bit is the breaks=c(-1,0.8,1.2,3) being your cutoffs.

like image 166
mathematical.coffee Avatar answered Nov 15 '22 13:11

mathematical.coffee