[R] color quantization / binning a variable into levels

David Forrest drf5n at maplepark.com
Tue Feb 21 18:08:38 CET 2006


Hi all,

I'd like to quantize a variable to map it into a limited set of integers
for use with a colormap.  "image" and filled.contour"  do this mapping
inside somewhere, but I'd like to choose the colors for plotting a set of
polygons.  Is there a pre-existing function that does something like this
well?  i.e., is capable of using 'breaks'?

quantize<-function(x,n=10, breaks=NULL){
# bin the variable x into n levels
  xmin<-min(x)
  xmax<-max(x)
  1+floor(n*(x-xmin)/(xmax-xmin)*.999)
}

x<- -10:10
cbind(x,quantize(x,2),quantize(x),quantize(x,21))

quantize(x,breaks=c(5,7))   #

Thanks for your time,

Dave
-- 
 Dr. David Forrest
 drf at vims.edu                                    (804)684-7900w
 drf5n at maplepark.com                             (804)642-0662h
                                   http://maplepark.com/~drf5n/




More information about the R-help mailing list