我有两个时间序列如下:
y1 <- mvrnorm(50, c(3,1), matrix(c(0.5,0.3,0.3,0.3),2,2))# 2-D bivariate normal
y2 <- mvrnorm(50, c(1,0), matrix(c(2,.1,.1,1),2,2))# another 2-D bivariate normal
y <- rbind(y1,y2) # append the second to the end of the first
我用ggplot绘制这些:
yd <- as.data.frame(y)
g<- ggplot(data=yd) +
geom_line(aes(x=1:nrow(yd), y=yd$V1, colour= "TS1"))+
geom_line(aes(x=1:nrow(yd), y=yd$V2, colour= "TS2"))+
scale_colour_manual(name= "Levels",
values = c("TS1"= "black",
"TS2" ="blue"))+
labs(title="Two time series")+
xlab("Time") +
ylab("Levels") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))
然后我运行一个分类器,它为每个时间点创建一个类标签的数字向量。下面我绘制后验并提供标签向量。
dput(labels)
c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L)
我希望能够针对从上述标签向量派生的类标签对图 1 进行颜色编码。明确地说,我希望能够在任何给定时间看到我处于什么状态(类),而不仅仅是看到状态转移的边界。我认为最直观的做法是在状态切换到 2 类时更改背景颜色(例如从灰色变为橙色)。
在 ggplot 中实现这一目标的最佳方法是什么?我显然对其他解决方案建议持开放态度。