[英]Dynamic colouring in R
I have the following R code that I am using to plot a set of time series, which are normalised [0,1]
. 我使用以下R代码来绘制一组时间序列,这些时间序列已标准化[0,1]
。 I want each of the lines to have a color intensity such that all values <= 0.5
have shades of blue and > 0.5
have shades of orange/yellow. 我希望每条线具有颜色强度,以使所有<= 0.5
值具有蓝色阴影,而> 0.5
具有橙色/黄色阴影。 This is what I have right now: 这就是我现在所拥有的:
data <- SOME_DATA
for(i in 1:length(data)){
y <- data[[paste("I",i,sep = "")]]
x <- 1:length(y)
lo <- loess(y~x)
ylim <- range(seq(-1,1,by=0.5))
if(i==1){
plot(x,y,ylim = ylim,main = "Some title",col = ifelse(y[1] < 0,'red','green'),ylab = "intensity",xlab = "time steps")
}
lines(predict(lo),lwd=2,ylim = ylim,col = ifelse(y[1] < 0,rgb(1,0,0,0.2),rgb(0,1,0,0.2)))
}
How can I go about achieving what I described above? 我如何才能实现上述目标?
Thanks 谢谢
Using this solution you can do the following: 使用此解决方案,您可以执行以下操作:
data <- data.frame(x= runif(100), y = runif(100))
color.gradient <- function(x, colors=c("blue","white","red"), colsteps=100) {
return( colorRampPalette(colors) (colsteps) [ findInterval(x, seq(min(x),max(x), length.out=colsteps)) ] )
}
plot(data$x, data$y, col= color.gradient(data$x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.