为数字向量的每个值指定颜色,范围从负到正

时间:2014-02-12 08:44:30

标签: r colors

我有一个数字向量

vector <- c(4.80,1.09,33.40,0.00,11.63,1.86,2.22,-10.95,-3.09,0.00,2.38,0.00,
            43.46,12.55,-49.51,0.00,0.00,0.00,1.72,0.00,0.00,-0.74,0.00,-5.79)

从-49.510到43.460不等。

我想从每个数字中为这个调色板分配颜色

palette <- colorRampPalette(colors=c("#FF0000", "#FFFF00"))

从“红色”到“黄色”不等。我希望为0.00指定调色板的中心值,将其他数字指定为“#FF0000”或“#FFFF00”的调色板值 - 分别为负数和正数 - 与它们与0.00的距离成正比。

这可能吗?我应该将矢量划分为间隔吗?

注意:我使用的是plot()

1 个答案:

答案 0 :(得分:1)

这可能不是答案,但这是我尝试过的。

    x<-c(4.80,1.09,33.40,0.00,11.63,1.86,2.22,-10.95,-3.09,0.00,2.38,0.00,
         43.46,12.55,-49.51,0.00,0.00,0.00,1.72,0.00,0.00,-0.74,0.00,-5.79)

    image(matrix(x), col=colorRampPalette(colors=c("#FF0000", "#FFFF00"))(length(x)))
    image(matrix(x), col=colorRampPalette(colors=c("#FF0000", "#FFFF00"))(length(unique(x))))

enter image description here 从上述评论中,尝试使用ggplot

x<-c(4.80,1.09,33.40,0.00,11.63,1.86,2.22,-10.95,-3.09,0.00,2.38,0.00,
     43.46,12.55,-49.51,0.00,0.00,0.00,1.72,0.00,0.00,-0.74,0.00,-5.79)

df<-data.frame(x=seq_along(x), y=10, col=x)
ggplot(data.frame(x=seq_along(x), y=1, col=x), aes(x, y, color=col)) + geom_point(size=8)+
  scale_colour_gradient2(low="red", high="yellow", mid="white")+
  theme(panel.background = element_rect(fill = "black"))
有趣的是,似乎因为高+/-接近零的那个并没有那么不同 enter image description here