我想使用ggplot2
为时间序列添加颜色标签。我有两个不同的状态(类),由下面的labels
向量给出,我想在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 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, 2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L, 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, 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))
如下所示,这个geom_ribbon
代码不是明确的红色和蓝色填充区域,而是产生两者的混合。
我在这里做错了什么?我认为这是fill
中的df_bg
列,但我并不完全确定。另外,如果我想在x轴上按日期绘图,我还可以使用geom_ribbon
来标记颜色吗?
P.S:这个问题与我之前的here有关。
答案 0 :(得分:2)
您需要为数据和group
电话添加geom_ribbon
参数。否则它按颜色分组,只使用每种颜色的最小值到最大值的填充颜色。
# 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))
编辑:要将x-axis
更改为Date
格式,您可以使用Date
命令中的geom_line
并更改df_bg
的x到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))