Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 是否可以基于变量为时间序列多色?_R_Time Series_Xts - Fatal编程技术网

R 是否可以基于变量为时间序列多色?

R 是否可以基于变量为时间序列多色?,r,time-series,xts,R,Time Series,Xts,我感兴趣的是,通过基于另一个变量对序列着色,将另一个维度的信息添加到timeseries图中。下面是我对xts软件包的尝试: library(xts) # create time series set.seed(100) tsvalues <- rnorm(100) dates <- seq(as.Date("2020-01-01"),length=100,by="days") xts1 <- xts(x=tsvalues, order.

我感兴趣的是,通过基于另一个变量对序列着色,将另一个维度的信息添加到timeseries图中。下面是我对xts软件包的尝试:

library(xts)
# create time series
set.seed(100)
tsvalues <- rnorm(100)
dates <- seq(as.Date("2020-01-01"),length=100,by="days")
xts1 <- xts(x=tsvalues, order.by=dates) 
# create the coloring variable and coloring value
colvar <- rnorm(50, 15, 5)
coloring <- colorRampPalette(terrain.colors(40))
colorval <- coloring(40)[colvar]
# plot
plot(xts1, col=colorval)
库(xts)
#创建时间序列
种子集(100)

tsvalues您可以使用

library(xts)
# create the coloring variable and coloring value
colvar <- rnorm(33, 15, 5)
coloring <- colorRampPalette(terrain.colors(33))
colorval <- coloring(33)[colvar]
# plot
plot(xts1, type="n")
Map(function(x, y) lines(xts1[x], col=colorval[y]), 
    as.data.frame(t(cbind(1:100, 1:100+1, 1:100+2, 1:100+3)[seq(1, 98, 3),])), 
    1:33)
库(xts)
#创建着色变量和着色值

colvar优雅而实用的解决方案!非常感谢。
# create time series
set.seed(100)
tsvalues <- rnorm(100)
dates <- seq(as.Date("2020-01-01"), length=100, by="days")
xts1 <- xts(x=tsvalues, order.by=dates)