[R] gradient generation

Jim Lemon jim at bitwrit.com.au
Wed Jul 6 14:10:50 CEST 2011


On 07/06/2011 04:19 PM, Annemarie Verkerk wrote:
> Dear R-help subscribers;
>
> I have a question regarding making gradients in R. I've searched on the
> web, but was only able to find functions that make a gradient between
> color X and Y, which is not what I want.
>
> I want to 'pick out' individual, smaller pieces of a gradient based on a
> range of numbers. Say that I have a range of numbers, leading from 0 to
> 1. Then I want 0 to refer to the brightest red, and 1 to the brightest
> blue, and all values in between refer to shades of purple. (So, 0.5
> would be regular purple, 0.7 would be a quite bluish purple, etc.)
>
> Then, I want to be able pick out a gradient from the larger 0-1
> gradient, say for instance the gradient between 0.25 and 0.35 and refer
> to this with an object name for further use.
>
> I'm not sure whether this is possible - maybe it would only be possible
> to define a range of individual R colors first that already form a
> gradient, and then 'pick out' the colors that you want to make the
> gradient.
>
Hi Annemarie,
You can do this with a number of functions. As I am most familiar with 
color.scale (plotrix), I'll explain with that.

# get the range of colors from 0 to 1 by 0.05
colorgrad01<-color.scale(seq(0,1,by=0.05),cs1=c(1,0),cs2=0,cs3=(0,1))
# now get your partial gradient
colorgrad.25.35<-colorgrad01[6:8]

In other words, create the complete color scale (as you might with the 
rainbow() function) and then pull out part of it to use. You could just 
index the entire color scale, of course.

Jim



More information about the R-help mailing list