ggplot2 geom_ribbon用于标记时间序列

时间:2015-02-09 10:31:02

标签: r ggplot2 time-series

我想使用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,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 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, 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代码不是明确的红色和蓝色填充区域,而是产生两者的混合。

enter image description here

我在这里做错了什么?我认为这是fill中的df_bg列,但我并不完全确定。另外,如果我想在x轴上按日期绘图,我还可以使用geom_ribbon来标记颜色吗?

P.S:这个问题与我之前的here有关。

1 个答案:

答案 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))