R ggplot基于类的彩色标签时间序列 [英] R ggplot colour labelling time series based on class

查看:221
本文介绍了R ggplot基于类的彩色标签时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间序列如下:

  y1 < -  mvrnorm(50,c(3,1),矩阵(c(0.5,0.3,0.3,0.3),2,2))#2 -D二元正态
y2 <-mvrnorm(50,c(1,0),矩阵(c(2,...))。 1,.1,1),2,2))#另一个二维二元正态
y< - rbind(y1,y2)#将第二个追加到第一个

我用ggplot绘制这些图:

  yd < -  as.data.frame(y)
g < - ggplot(data = yd)+
geom_line(aes(x = 1:nrow(yd),y = yd $ V1 ,颜色=TS1))+
geom_line(aes(x = 1:nrow(yd),y = yd $ V2,color =TS2))+
scale_colour_manual(name =Levels ,
values = c(TS1=black,
TS2=blue))+
labs(title =Two time series)+
blab(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, ,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, b 2L,2L,2L,2L,2L)

我希望能够对图1相对于从上述标签向量导出的类别标签。清楚的是,我希望能够在任何时候看到我所处的状态(类),而不仅仅是看到状态转换的边界。我认为最直观的做法是当状态转移到第2级时,改变背景颜色(例如从灰色到橙色)。

在ggplot中实现这个最好的方法是什么?我明显对其他解决方案建议持开放态度。

 #创建背景数据
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 = 0.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)+
blab(Time)+
ylab(Levels)+
theme(legend.justification = c(1,0),legend.position = c(1,0))


I have two time series as below:

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

I plot these with 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))

I then run a classifier which creates a numeric vector of class labels for each time point. Below I plot the posteriors and provide the label vector.

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)

I want to be able to colour-code Figure 1 with respect to the class labels derived from the above labels vector. To be clear, I want to be able to see what state (class) I am in at any given time, rather than just seeing the state shift boundaries. The most intuitive way of doing that I think is to change the background colour (e.g. from gray to orange) when state shifts to class 2.

What is the best way of achieving this in ggplot? I am obviously open to other solution suggestions.

解决方案

You can add something like a background color with 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)) 

这篇关于R ggplot基于类的彩色标签时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆