ggplot2 geom_ribbon到颜色标签时间序列 [英] ggplot2 geom_ribbon to colour-label time series
问题描述
我想用 ggplot2
来给一个时间序列添色。我有两个不同的状态(类),如下面的标签
向量所给出的,我想要在IBM Close
价格。
以下是我的尝试,很遗憾,无法按状态生成颜色区域。我主要使用 geom_ribbon()
:
library( TTR)
library(ggplot2)
data <-getYahooData(IBM,start = 20130101,end = 20150101,freq =daily)
df < - data.frame(data)
df $ Date< -as.Date(row.names(df),%Y-%m-%d)
#plot color coded (标号)
c(NA,NA,NA,NA,NA,NA,NA,NA,NA,1L,1L,1L,1L,1L,1L,
1L,2L,2L,2L,2L,2L,2L,2L,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,1L, 1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,
2L, 1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,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,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, $ b 2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,2L,2L,2L,2L,2L, 2L,2L,2L,2L,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,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L, 1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,1L,
1L,1L,1L,1L,1L,1L,1L, 1L,1L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,2L,2L,1L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,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,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,
2L,2L,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,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,2L,2L,2L,2L,2L,2L, 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, 1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,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)
#geom_ribbon使用的颜色和类标签
df_bg< - data.frame(x = c(0,rep (as.logical(diff(labels))),each = 2),length(labels)),
ymin = min(df $ Close,na.rm = TRUE),
ymax = 1.1 * max(df $ Close,na.rm = TRUE),
fill = factor(rep(labels [c(which(as.logical(diff(labels))),length(labels))] ,每个= 2))
)
ggplot(data = df)+
geom_line(aes(x = 1:nrow(df),y = diff_ma))+
labs(title =IBM 2-State HMM)+
geom_ribbon(data = df_bg,
aes(x = x,ymin = ymin,ymax = ymax,fill = fill),alpha = 0.2)+
xlab(Date)+
ylab(Levels)+
theme(legend.justification = c(1,0),legend.position = c 1,0))
正如您在下面看到的,不是明显的红色和蓝色填充区域, geom_ribbon
代码会产生两者的混合。 b
我在这里做错了什么?我认为它是 df_bg
中的 fill
列,但我不完全确定。另外,如果我想在x轴上按日期进行绘图,是否仍然可以使用 geom_ribbon
来标记颜色?
PS:这个问题与我以前的一个这里。
您需要为您的组添加
参数数据和 geom_ribbon
呼叫。否则,它按颜色分组,并使用每种颜色的最小值到最大值的填充颜色。
#geom_ribbon使用的颜色和类标签
df_bg< - data.frame(x = c( 0,rep(其中(as.logical(diff(labels))),each = 2),length(labels)),
ymin = min(df $ Close,na.rm = TRUE),
ymax = 1.1 * max(df $ Close,na.rm = TRUE),
fill = factor(rep(labels [c(which(as.logical(diff(labels))),length(labels)) ],
grp = factor(rep(seq(sum(as.logical(diff(labels)),na.rm = TRUE)+1),each = 2) )
)
#
ggplot(data = df)+
geom_line(aes(x = 1:nrow(df),y = Close))+
labs (title =IBM 2-State HMM)+
geom_ribbon(data = df_bg,
aes(x = x,ymin = ymin,y = 180,ymax = ymax,fill = fill,group = grp),alpha = .2)+
xlab(Date)+
ylab(Levels)+
theme(legend.justification = c(1,0),legend。 position = c(1,0))
编辑:为了改变 x轴
至日期
格式,您可以使用日期
在 geom_line
命令中,并将 df_bg
的x更改为 Date
。
#geom_ribbon使用的颜色和类标签
df_bg< - data.frame(x = df [ c(1,rep(which(as.logical(diff(labels))),each = 2),length(labels)),Date],
ymin = min(df $ Close,na.rm = TRUE),
ymax = 1.1 * max(df $ Close,na.rm = TRUE),
fill = factor(rep(labels [c(which(as.logical(diff(labels))) ),
grp = factor(rep(seq(sum(as.logical(diff(labels)),na.rm = TRUE)+ 1),each = 2))
)
#
ggplot(data = df)+
geom_line(aes(x = Date,y = Close))+
实验室(title =IBM 2-State HMM)+
geom_ribbon(data = df_bg,
aes(x = x,ymin = ymin,y = 180,ymax = ymax,fill = fill, group = grp),alpha = .2)+
xlab(Date)+
ylab(Levels)+
theme(legend.justification = c(1,0), legend.position = c(1,0))
I want to colour-label a time series using ggplot2
. I have two distinct states (classes) as given by the labels
vector below and I want to super-impose this on top of a plot of IBM Close
prices.
Below is my attempt, which unfortunately can not produce distinctively colour-coded regions by state. I am mainly having trouble with using geom_ribbon()
:
library(TTR)
library(ggplot2)
data <- getYahooData("IBM", start = 20130101, end = 20150101, freq = "daily")
df <- data.frame(data)
df$Date <-as.Date(row.names(df),"%Y-%m-%d")
# plot colour coded states on top of the original signal
dput(labels)
c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 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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)
# colour and class labels to be used by geom_ribbon
df_bg <- data.frame(x = c(0, rep(which(as.logical(diff(labels))), each=2), length(labels)),
ymin = min(df$Close, na.rm = TRUE),
ymax = 1.1*max(df$Close, na.rm = TRUE),
fill = factor(rep(labels[c(which(as.logical(diff(labels))), length(labels) )], each=2))
)
ggplot(data=df) +
geom_line(aes(x=1:nrow(df), y=diff_ma )) +
labs(title="IBM 2-State HMM") +
geom_ribbon(data = df_bg,
aes(x = x, ymin=ymin, ymax=ymax, fill=fill), alpha=.2) +
xlab("Date") +
ylab("Levels") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))
As you can see below, rather than distinct red and blue filled regions, this geom_ribbon
code produces a mixture of the two.
What am I doing wrong here? I think it is the fill
column in df_bg
, but I am not entirely sure. Also, if I wanted to plot by Date in the x axis, can I still use geom_ribbon
to colour label?
P.S: This question is related to my previous one here.
You need to add a group
argument to your data and geom_ribbon
call. Otherwise it groups by color and just uses the filling color from the minimum to the maximum value for each of your colors.
# colour and class labels to be used by geom_ribbon
df_bg <- data.frame(x = c(0, rep(which(as.logical(diff(labels))), each=2), length(labels)),
ymin = min(df$Close, na.rm = TRUE),
ymax = 1.1*max(df$Close, na.rm = TRUE),
fill = factor(rep(labels[c(which(as.logical(diff(labels))), length(labels) )],
each=2)),
grp = factor(rep(seq(sum(as.logical(diff(labels)), na.rm=TRUE)+1), each=2))
)
#
ggplot(data=df) +
geom_line(aes(x=1:nrow(df), y=Close)) +
labs(title="IBM 2-State HMM") +
geom_ribbon(data = df_bg,
aes(x = x, ymin=ymin, y=180, ymax=ymax, fill=fill, group=grp), alpha=.2) +
xlab("Date") +
ylab("Levels") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))
EDIT: In order to change the x-axis
to Date
format, you can use the Date
in your geom_line
command and change the x of your df_bg
to a Date
.
# colour and class labels to be used by geom_ribbon
df_bg <- data.frame(x = df[c(1, rep(which(as.logical(diff(labels))), each=2), length(labels)), "Date"],
ymin = min(df$Close, na.rm = TRUE),
ymax = 1.1*max(df$Close, na.rm = TRUE),
fill = factor(rep(labels[c(which(as.logical(diff(labels))), length(labels) )],
each=2)),
grp = factor(rep(seq(sum(as.logical(diff(labels)), na.rm=TRUE)+1), each=2))
)
#
ggplot(data=df) +
geom_line(aes(x=Date, y=Close)) +
labs(title="IBM 2-State HMM") +
geom_ribbon(data = df_bg,
aes(x = x, ymin=ymin, y=180, ymax=ymax, fill=fill, group=grp), alpha=.2) +
xlab("Date") +
ylab("Levels") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))
这篇关于ggplot2 geom_ribbon到颜色标签时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!