[R] Color2D.matplot uniform color range

Jim Lemon jim at bitwrit.com.au
Mon Dec 12 09:36:47 CET 2011


On 12/12/2011 02:48 PM, jalfaro wrote:
> Hi all.
> I am having a problem using color2D.matplot
> I am trying to visualize several different matrices under two color ranges.
> One color range corresponds to values less than 1 and the second color range
> for values greater than 1. However, the minimum value of each matrix differs
> and is automatically set to have the smallest value in the color range.
> Similarly, the maximum value of each matrix differs leading to non-uniform
> color scales.
>
> Below is the function that is causing me trouble. It is designed to take as
> input 2  csv files with row and column headers describing a 20X20 matrix.
> The goal would be to modify the code below as follows.
> (1) if an entry is in range [0,1] the color scale should always be uniform
> with the minimum color being 0.5 and the maximum color being 1.
> (2) if an entry is in the range [1,infinity]  the color scale should have
> minimum color value 1 and maximum color value 3. (I used infinity simply
> because there is no way of knowing how high the value will be beforehand).
>
> Any help would be greatly appreciated.
>
> Thanks,
> Jav
>
> library(plotrix)
> make_S_figure<-function(filename,alias){
>   h0<- read.csv(file=filename,head=TRUE,sep=",",row.names=1)
>   d =data.matrix(h0)
>   m<- 1:20
>   cellcolors<-matrix(NA,nrow=20,ncol=20)
>   cellcolors2<--matrix(NA,nrow=20,ncol=20)
>   for(i in 1:length(m)){
>
>   cellcolors[d>= 1]<-color.scale(d[d>=1],cs1=c(1,1),cs2=c(1,0),cs3=c(1,0))
>   cellcolors[d<1]<-color.scale(d[d<1],cs1=c(0,1),cs2=c(0,1),cs3=c(1,1))
>
> color2D.matplot(d,cellcolors=cellcolors,show.values=F,na.color="white",axes=FALSE,main=alias,
> xlab="",ylab="")
>   axis(1,at=0.5:19.5,labels=colnames(d))
>   axis(2,at=0.5:19.5,labels=rev(rownames(d)))
>   }
>
>
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



More information about the R-help mailing list