Skip to content

color quantization / binning a variable into levels

2 messages · David Forrest, Adelchi Azzalini

#
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
#
On Tue, 21 Feb 2006 11:08:38 -0600 (CST), David Forrest wrote:
perhaps "binning" of package "sm" is what you want

best wishes,
Adelchi Azzalini

DF> Hi all,
DF> 
DF> I'd like to quantize a variable to map it into a limited set of
DF> integers for use with a colormap.  "image" and filled.contour"  do
DF> this mapping inside somewhere, but I'd like to choose the colors
DF> for plotting a set of polygons.  Is there a pre-existing function
DF> that does something like this well?  i.e., is capable of using
DF> 'breaks'?
DF> 
DF> quantize<-function(x,n=10, breaks=NULL){
DF> # bin the variable x into n levels
DF>   xmin<-min(x)
DF>   xmax<-max(x)
DF>   1+floor(n*(x-xmin)/(xmax-xmin)*.999)
DF> }
DF> 
DF> x<- -10:10
DF> cbind(x,quantize(x,2),quantize(x),quantize(x,21))
DF> 
DF> quantize(x,breaks=c(5,7))   #
DF> 
DF> Thanks for your time,
DF> 
DF> Dave