简体   繁体   English

为数字矢量的每个值分配颜色,范围从负到正

[英]Assign color to each value of numeric vector ranging from negative to positive

I have a numeric vector 我有一个数值向量

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)

ranging from -49.510 to 43.460. 从-49.510到43.460。

I would like to assign to each number a color from this palette 我想为每个数字分配此调色板中的一种颜色

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

ranging from "red" to "yellow". 从“红色”到“黄色”。 I want 0.00 to be assigned the central value of the palette and the other numbers a palette value toward "#FF0000" or "#FFFF00" - respectively for negative and positive number - proportional to their distance from 0.00. 我希望为0.00分配调色板的中心值,而其他数字则向“#FF0000”或“#FFFF00”分配调色板值-分别为负数和正数-与它们与0.00的距离成比例。

Is that possible? 那可能吗? Should I divide my vector into intervals? 我应该将向量划分为间隔吗?

NOTE: I am using plot() 注意:我正在使用plot()

It may not be the answer but this is what I tried. 可能不是答案,但这就是我尝试过的方法。

    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))))

在此处输入图片说明 from the above comments, trying with ggplot 从以上评论中,尝试使用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"))

interesting, seems because of high +/- the one close to zero are not that different 有趣的是,似乎由于高+/-,接近零的值没什么不同 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM