【发布时间】:2015-04-06 09:19:30
【问题描述】:
我有两个时间序列如下:
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 中实现这一目标的最佳方法是什么?我显然愿意接受其他解决方案建议。
【问题讨论】:
-
在时移时用
geom_vline()画一条垂直线? -
但是如果我假设 3 个状态,我将不知道是从状态 1 到状态 3 还是从状态 1 到状态 2 的转变。(我明白你的意思,但会将其添加到问题)
标签: r ggplot2 time-series classification timeserieschart