Skip to content
Prev 280012 / 398506 Next

Color2D.matplot uniform color range

On 12/12/2011 02:48 PM, jalfaro wrote:
Hi jalfaro,
I'm not sure what you mean by a "uniform color range", so I'll provide a 
couple of solutions. For your values between zero and one, the code 
above should go from blue (0) to white (1). If the "minimum color" means 
that anything from 0 to 0.5 should be blue and anything from 0.5 to 1 
should grade from blue to white, specify your colors like this:

# you don't need a loop for this
cellcolors[d<1]<-color.scale(d[d<1],cs1=c(0,0,1),cs2=c(0,0,1),cs3=1)

The above will give you blue from 0 to 0.5, then scale to white at 1. 
What I think you want for the values of 1 and above is to scale from 
white to red between 1 and 3, then have red for all values above 3. So:

# you don't need a loop for this either
cellcolors[d >= 1 & d < 3]<-
  color.scale(d[d >= 1 & d < 3],cs1=1,cs2=c(1,0),cs3=c(1,0))
# no point in calling color.scale
cellcolors[d >= 3]<-"red"

Now, rereading your message, I think that you want to anchor the 
extremes of the scales regardless of the values in the matrix. If so, 
try this:

# add the extreme values, then drop the resulting extreme colors
# in the assignment to the color matrix
cellcolors[d<1]<-
  color.scale(c(0,1,d[d<1]),cs1=c(0,0,1),cs2=c(0,0,1),cs3=1)[1:2]
# same trick here, add the extremes, then drop them
cellcolors[d >= 1 & d < 3]<-
  color.scale(c(1,3,d[d >= 1 & d < 3]),
  cs1=1,cs2=c(1,0),cs3=c(1,0))[-1:2]
# and fill in everything greater than 3
cellcolors[d >= 3]<-"red"

Let me know if you have any problems.

Jim