1

I need a gradient function for two "sides", let me explain:

Example:
x <- c(1,4,3,4,5,6,1,8,9,3)
then I need a array of color, the number 5 is the main color blue and the smaller and larger numbers are the gradient to red.

I found this

colorRampPalette(c("red", "blue"))

but if I do:

colfunc <- colorRampPalette(c("red", "blue"))
colfunc(10)

Returns a color progression from red to blue ignoring the value...

How can I do this?

Ps: I don't use any library...

Lucas Prestes
  • 321
  • 4
  • 17

1 Answers1

0

For share with the community,

using the function "colorRampPalette", like:

col <- colorRampPalette(c('red', 'blue','red'))(10)[y]

where 10 is the range 1:10 with extremities red and in middle (5) in blue and other values are the gradient going to red.

in an example,

size <- 30 #number of value in the data vector
x <- c(1:size)
y <- c(1  ,2  ,3.5,3  ,4,
   5  ,5.5,6  ,6.3,7,
   5  ,2  ,1  ,0.5,1.6,
   2.3,5.7,6  ,7  ,7.5,
   8  ,9  ,11 ,12 ,10,
   7  ,6.4,5  ,4.6  ,4)


col <- colorRampPalette(c('red', 'blue','red'))(12)[y]

plot(x, y) # draw the points, in black 
for (i in 1:size-1) # draw the segments in colour 
    lines(x[i:(i+1)], y[i:(i+1)], type='o', pch=16, col=col[i]) 

Image with extremities values in red and middle values in blue.

Lucas Prestes
  • 321
  • 4
  • 17