簡體   English   中英

ggplot2 geom_ribbon至顏色標簽的時間序列

[英]ggplot2 geom_ribbon to colour-label 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,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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, 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代碼產生了兩者的混合,而不是紅色和藍色填充的區域不同。

在此處輸入圖片說明

我在這里做錯了什么? 我認為這是df_bgfill列,但我不確定。 另外,如果我想在x軸上按日期繪制,是否仍可以使用geom_ribbon來給標簽geom_ribbon顏色?

PS:這個問題與我先前在這里提出的問題有關。

您需要在數據和geom_ribbon調用中添加一個group參數。 否則,它會按顏色分組,並且只使用每種顏色從最小值到最大值的填充色。

# 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-axisDate格式,您可以使用Dategeom_line命令,改變你的x df_bgDate

# 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM