David Forrest
2006-Feb-21 17:08 UTC
[R] color quantization / binning a variable into levels
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/
Adelchi Azzalini
2006-Feb-22 09:09 UTC
[R] color quantization / binning a variable into levels
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 -- Adelchi Azzalini <azzalini at stat.unipd.it> Dipart.Scienze Statistiche, Universit?? di Padova, Italia tel. +39 049 8274147, http://azzalini.stat.unipd.it/