嗨我有一个有三列的数据
我想根据状态绘制具有不同颜色阴影区域的关闭与日期图表。
Price<-data.frame(y,NTRI$NTRI.Close[15:nrow(NTRI),],HMMpost[,1])
colnames(Price) <- c( "Date","Close", "State")
xstart<- as.Date(Price$Date[[1]])
xend<- as.Date(Price$Date[[nrow(Price)]])
rects<- data.frame(xstart= xstart, xend= xend, col= letters[1:3])
ggplot() +
geom_line(data = Price, aes(x = Date, y = Close)) +
geom_rect(data = rects,
aes(xmin = xstart, xmax = xend,
ymin = 0, ymax = +Inf, fill = col),
alpha = 0.5)
structure(list(Close = c(22.89, 23.51, 23.45, 24.72, 26.76, 27.74, 28.62, 26.9, 30.58, 28.79, 27.25, 27.44, 23.79, 24.26, 23.33, 25.26, 24.89, 23.11, 23.89), Date = structure(c(13901, 13902, 13903, 13906, 13907, 13908, 13909, 13910, 13913, 13914, 13915, 13916, 13917, 13920, 13921, 13922, 13923, 13924, 13928), class = "Date"), State = c(3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3)), class = "data.frame", row.names = c(NA, -19L))