简体   繁体   English

R中的动态着色

[英]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))

R图预览

Slightly more configurable image may be possible with ggplot. ggplot可能会稍微配置一些图像。 Using data and color.gradient from @jakobr : 使用@jakobr的data和color.gradient:

ggplot(data, aes(x, y))+ geom_point(col=color.gradient(data$x))+ theme_bw()

在此处输入图片说明

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

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