根据值的向量,绘图中的颜色点有所不同 [英] Colour points in a plot differently depending on a vector of values

查看:123
本文介绍了根据值的向量,绘图中的颜色点有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R的绘图函数绘制下面的图。它是时间移动的矢量shiftTime的图。我有另一个矢量'强度'的强度值范围从〜3到〜9。我想根据这些值用颜色渐变来绘制图中的点。例子我可以在实际绘制的点的值上找到颜色,因此在这种情况下是矢量'shiftTime'的值。也可以使用不同的向量,只要相应的值在同一个索引上?

I'm plotting the plot below using R's plot function. It is a plot of a vector 'shiftTime' of shift in time. I have another vector 'intensity' of the intensity values ranging from ~3 to ~9. I want to color my points in the plot based on those values with a color gradient. The examples I can find color on the value of the actual plotted points, so in this case the values of the vector 'shiftTime'. Is it also possible to use a different vector, as long as the corresponding values are on the same index?

推荐答案

p>

Here's a solution using base R graphics:

#Some sample data
x <- runif(100)
dat <- data.frame(x = x,y = x^2 + 1)

#Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('red','blue'))

#This adds a column of color values
# based on the y values
dat$Col <- rbPal(10)[as.numeric(cut(dat$y,breaks = 10))]

plot(dat$x,dat$y,pch = 20,col = dat$Col)

>

这篇关于根据值的向量,绘图中的颜色点有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆