[R] Setting a Red->Yellow->Green Color Transition in Image(...) with yellow corresponding with the Mean Value...

Duncan Murdoch murdoch.duncan at gmail.com
Sat Nov 20 15:11:10 CET 2010


On 20/11/2010 8:53 AM, Jason Rupert wrote:
> In the following example I would like to have the color transition from
> Red->yellow->green in the image(...) function, and if possible have the yellow
> be set to correspond with the mean value (for the below 75).

You can get the colour transition using the colorRamp or 
colorRampPalette functions.  For example, to get 12 colours from red 
through yellow to green, use

colorRampPalette(c("red", "yellow", "green"), space="Lab")(12)

This will put yellow in the middle of the range.  If the mean isn't in 
the middle, you'll need something a little more complicated involving 
colorRamp.  It returns a function mapping the numbers 0 to 1 into the 
colours you specify; you'll need to write your own function that maps 
(min, mean, max) to (0, 0.5, 1) in some way.

Duncan Murdoch


>
>
> I was able to figure something like out when using filled.contour(...), e.g.
> filled.contour(ak.fan, levels = c(70, 73, 76, 80, 90), color = terrain.colors)
> (I still couldn't get the Red->yellow->green transition figured out for this
> example...ugh...need more caffeine)
>
> I am hoping a similar amount of control is available using image:
>
> library(akima)
>
> hyp_distance<-seq(1,15)
> angle_deg_val<-seq(0,15)
>
>
> x_distance_val<-NULL
> y_distance_val<-NULL
>
> for(ii in 1:length(hyp_distance))
> {
>         for(jj in 1:length(angle_deg_val))
>         {
>                 x_distance_tmp<-hyp_distance[ii]*cos(angle_deg_val[jj]*pi/180)
>                 y_distance_tmp<-hyp_distance[ii]*sin(angle_deg_val[jj]*pi/180)
>
>                 x_distance_val<-c(x_distance_val, x_distance_tmp)
>                 y_distance_val<-c(y_distance_val, y_distance_tmp)
>         }
> }
>
> temperature_vals<-rnorm(length(x_distance_val), 75, 2.2)
>
> temp_samples<-cbind(x_distance_val, y_distance_val, temperature_vals)
> ak.fan<- interp(temp_samples[,1],temp_samples[,2],temp_samples[,3] )
> filled.contour(ak.fan, levels = c(70, 73, 76, 80, 90), color = terrain.colors)
>
> image(ak.fan)
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list