[R] definition of colorpalette

David Winsemius dwinsemius at comcast.net
Tue Sep 21 17:02:41 CEST 2010


On Sep 21, 2010, at 10:50 AM, Daniel Stepputtis R wrote:

> Dear group,
>
> I have recognized a strange behaviour of palette(). I tried to find  
> any explanation but failed so far (or even didnt understood the idea  
> behind - what is most probable).
>
> My original plan was to define a palette, save it in a variable and  
> use it later for an image-plot. One reason why I tried to store the  
> palette in a variable was, because I wanted to change individual  
> values (e.g. the first value to gray).
>
> Interestingly, the palette is not defined correctly in the first  
> run, but in the second run.

No, you have misunderstood what was happening. Read the Value section  
of hte help page. The returned value of a call to palette is the _old_  
settings, (just like the par function). It allows a programming  
strategy like:

oldvals <-func_with_side_effect(new_settings)
<Do stuff with new_settings in place>
func(oldvals)


> Simple example:
>
> >rm(list=ls())
> > a <- palette(rainbow(6))
> > a
> [1] "red"     "#FF4C00" "#FF9900" "#FFE500" "#CCFF00" "#80FF00"  
> "#33FF00" "#00FF19" "#00FF66" "#00FFB2" "cyan"    "#00B3FF"  
> "#0066FF" "#0019FF" "#3300FF" "#8000FF" "#CC00FF"
> [18] "#FF00E6" "#FF0099" "#FF004D"
> > a <- palette(rainbow(6))
> > a
> [1] "red"     "yellow"  "green"   "cyan"    "blue"    "magenta"
>
> ###################
> Interestingly, this works at the first time
> > palette(rainbow(20))     # six color rainbow
> > plot(rnorm(20),col=1:20)
>
> as well as
> > palette(rainbow(6))
> > a <- palette()
> > a
> [1] "red"     "yellow"  "green"   "cyan"    "blue"    "magenta"
>
> So, it seems to be that first a palette has to be defined (or set as  
> to be used) and then the vector can be assigned to a variable. I  
> dont understand why.
>
> Thank you in advance for your help and explanation.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list