设置R中矢量的颜色缩放比例

时间:2015-01-07 03:20:34

标签: r colors

我想在我选择的色阶上采用0到1之间的数值向量。现在,当有两种颜色时,我可以通过以下代码获得控制权。

library(plotrix)
set.seed(1)
x <- sort(runif(10, min = 0, max = 1))
redsNblues<- color.scale(x, extremes = c("#ff0000", "#0000ff"))
plot(x, col = redsNblues)

但是,如果我想从蓝色变为红色并且白色介于两者之间,我将如何优雅地做到这一点?

1 个答案:

答案 0 :(得分:3)

library(plotrix)
set.seed(1)
x <- sort(runif(10, min = 0, max = 1))

# Create a color function that will return colors in the range we want
colorfunc = colorRamp(c("blue","white","red"))

# Use colorfunc to create colors that range from blue to white to red 
# across the range of x
mycolors = rgb(colorfunc(x), maxColorValue=255)

plot(x, col=mycolors, pch=16, cex=3)

enter image description here