[R] setting color ranges

Jim Lemon jim at bitwrit.com.au
Fri Feb 15 12:56:06 CET 2008


samsr wrote:
> Hi,
> 
>  I need to plot a matrix using image() such that negative values are easily
> distinguishable from posittive values, while also maintaining a gradation in
> color with magnitude. How can I set ranges for colors in order to achieve
> this. Thanks.
> 
Hi Sam,

This will give you red colors for the negative values, green for positive.

x<-matrix(rnorm(100),nrow=10)
cellcol<-matrix(rep("#000000",100),nrow=10)
library(plotrix)
cellcol[x<0]<-color.scale(x[x<0],c(1,0.8),c(0,0.5),0)
cellcol[x>0]<-color.scale(x[x>0],0,c(0.7,0.8),c(0.5,0))
image(z=x,col=cellcol)

Jim



More information about the R-help mailing list