基于class的R ggplot颜色标签时间序列
R ggplot colour labelling time series based on class
我有两个时间序列如下:
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))
然后我 运行 一个 classifier 为每个时间点创建一个 class 标签的数字向量。下面我绘制后验并提供标签向量。
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)
我希望能够根据从上述标签向量派生的 class 标签对图 1 进行颜色编码。明确地说,我希望能够看到我在任何给定时间处于什么状态 (class),而不仅仅是看到状态转移边界。我认为最直观的做法是 在状态转换为 class 时更改背景颜色(例如从灰色变为橙色)2.
在 ggplot 中实现此目的的最佳方法是什么?我显然对其他解决方案建议持开放态度。
您可以使用 geom_ribbon
添加背景颜色之类的东西。
# creating background data
df_bg <- data.frame(x = c(0, rep(which(as.logical(diff(labels))), each=2), length(labels)),
ymin = 1.1*min(yd$V1, yd$V2),
ymax = 1.1*max(yd$V1, yd$V2),
fill = factor(rep(unique(labels), each=2)))
# plot
g <- ggplot(data=yd, aes(x = seq_along(V1))) +
geom_ribbon(data = df_bg,
aes(x = x, ymin=ymin, ymax=ymax, fill=fill), alpha=.2) +
geom_line(aes(y=V1, color="TS1")) +
geom_line(aes(y=V2, color="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))
我有两个时间序列如下:
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))
然后我 运行 一个 classifier 为每个时间点创建一个 class 标签的数字向量。下面我绘制后验并提供标签向量。
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)
我希望能够根据从上述标签向量派生的 class 标签对图 1 进行颜色编码。明确地说,我希望能够看到我在任何给定时间处于什么状态 (class),而不仅仅是看到状态转移边界。我认为最直观的做法是 在状态转换为 class 时更改背景颜色(例如从灰色变为橙色)2.
在 ggplot 中实现此目的的最佳方法是什么?我显然对其他解决方案建议持开放态度。
您可以使用 geom_ribbon
添加背景颜色之类的东西。
# creating background data
df_bg <- data.frame(x = c(0, rep(which(as.logical(diff(labels))), each=2), length(labels)),
ymin = 1.1*min(yd$V1, yd$V2),
ymax = 1.1*max(yd$V1, yd$V2),
fill = factor(rep(unique(labels), each=2)))
# plot
g <- ggplot(data=yd, aes(x = seq_along(V1))) +
geom_ribbon(data = df_bg,
aes(x = x, ymin=ymin, ymax=ymax, fill=fill), alpha=.2) +
geom_line(aes(y=V1, color="TS1")) +
geom_line(aes(y=V2, color="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))