R中的两个方向的梯度

时间:2018-01-10 02:26:12

标签: r colors gradient

我需要两个“边”的渐变功能,让我解释一下:

例:
    <div> <div class="inlineItems" id="progBtnBar"> <div class="innerDiv1"> <div class="inner2"> <div id="myProgress"> <div class="rect">0%</div> </div> </div> </div> <div class="innerDiv2"> <button class=sendreceiveBtn>Transmit</button> </div> </div> </div>
然后我需要一个颜色数组,数字5是主要颜色蓝色,较小和较大数字是渐变为红色。

我找到了this

x <- c(1,4,3,4,5,6,1,8,9,3)

但如果我这样做:

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

返回从红色到蓝色的颜色渐变,忽略值...

我该怎么做?

Ps:我不使用任何图书馆......

1 个答案:

答案 0 :(得分:0)

要与社区共享,

使用功能“ colorRampPalette”,例如:

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

其中10是范围1:10,四肢为红色,中间(5)为蓝色,其他值为渐变为红色。

在一个示例中

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.

相关问题