[R] color quantization / binning a variable into levels

David Forrest drf5n at maplepark.com
Wed Feb 21 23:24:09 CET 2007


My apologies for the tremendously tardy reply, but I ran into a 
similar problem and discovered findInterval and colorRampPallet 
as an alternative and though it might be worthy of a RSiteSearch() result 
for someone.

?findInterval
?colorRampPalette

jet.colors <-
        colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                           "#7FFF7F", "yellow", "#FF7F00", "red", 
"#7F0000"))

c.min<-range(volcano)[1]
c.max<-range(volcano)[2]
c.n<-200
c.brks<-seq(c.min,c.max,length=c.n)
c.ramp<-jet.colors(c.n)
c.ramp[findInterval(c.min,c.brks)]<-NA  #  make the first one transparent

# hack levelplot() or image()
plot(expand.grid(1:87,1:61),
   col=c.ramp[findInterval(volcano,c.brks,all.inside=TRUE)],
   pch='.',cex=10)

Dave

On Wed, 22 Feb 2006, Adelchi Azzalini wrote:

> 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
>
>

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