【问题标题】:Different color in the same price plot同一价格图中的不同颜色
【发布时间】:2021-05-26 16:12:32
【问题描述】:

我必须根据kmeans 函数给出的集群对价格图进行不同的着色。考虑这段代码:

library(tseries)

####

nomiequity <- "^IXIC" 
datastart  <- "2019-09-18"

nsdq.prices <- get.hist.quote(instrument  = nomiequity,    
                              compression = "d",         
                              start       = datastart, 
                              end         = "2020-12-31", 
                              retclass    = "zoo",
                              quote       = "AdjClose")

b<-kmeans(nsdq.prices,3)
c<-b$cluster
d<- merge(nsdq.prices, c)
e<-split(nsdq.prices, c)

plot(nsdq.prices, type="l", col="green", ylim=c(6000, 13000))
    lines(e[["2"]], type = "l", col="red")
    lines(e[["3"]], type = "l", col="blue")

结果几乎是我需要做的,但我不想在不同的时间显示相同颜色之间的那些联系。

【问题讨论】:

    标签: r plot time-series k-means price


    【解决方案1】:

    问题是集群在lines 中合并。当集群在时间序列中发生变化时,您可以使用 rle 长度将数字增加一。为此使用Maprep吃连续数字l 次。然后您可以在这些不断增长的数字上使用split,但使用cluster 来定义lines 的颜色。对于后者,使用lapply 循环分割的e

    cl <- kmeans(nsdq.prices, 3)$cluster
    l <- rle(as.numeric(cl))$lengths
    s <- Map(rep, seq(l), l)
    e <- split(cbind(nsdq.prices, cl), unlist(s))
    
    plot(nsdq.prices, type="l", col=7, ylim=c(6000, 13000))
    invisible(lapply(e, function(x) lines(x$Adjusted, col=x$cl + 1)))
    legend("topleft", leg=c(sprintf("cl %s", 1:3), "missing"), col=c((1:3)+1, 7), lty=1)
    

    如果没有定义日期,就会出现空白。我们可以使用zoo 插值,通过使用“缺失”颜色对原始图进行重叠绘制。

    【讨论】:

    • 谢谢 jay.sf!所以如果不分离各个部分就没有办法保持图表的原始形状,对吧?
    • @Dani.Tav 可能不会,因为一行是一个对象,需要不同颜色时需要分开。
    • @Dani.Tav 或者,如果您指的是空白,我稍微更改了代码以获得连续线。
    • 哦,我明白了。我想我会研究这个解决方案。谢谢!
    【解决方案2】:

    希望这段代码对你有帮助:

    library(tseries)
    
    ####
    
    nomiequity <- "^IXIC" 
    datastart  <- "2019-09-18"
    
    nsdq.prices <- get.hist.quote(instrument  = nomiequity,    
                                  compression = "d",         
                                  start       = datastart, 
                                  end         = "2020-12-31", 
                                  retclass    = "zoo",
                                  quote       = "AdjClose")
    
    b<-kmeans(nsdq.prices,3)
    c<-b$cluster
    d<- merge(nsdq.prices, c)
    e<-split(nsdq.prices, c)
    
    
    e3= e[['3']]
    empty <- zoo(order.by=seq.Date(head(index(e3),1),tail(index(e3),1),by="days"))
    e3=merge(e3,empty)
    
    
    e2= e[['2']]
    empty <- zoo(order.by=seq.Date(head(index(e2),1),tail(index(e2),1),by="days"))
    e2=merge(e2,empty)
    
    
    plot(nsdq.prices, type="l", col="green", ylim=c(6000, 13000))
    lines(e2, type = "l", col="red")
    lines(e3, type = "l", col="blue")
    

    我刚刚用过这个:R: Filling missing dates in a time series?

    【讨论】:

    • 谢谢LocoGris!但是生成的情节和我的一样。问题是,在同一“行”中的观察结果应该用不同的颜色显示,而不显示同一簇的非相邻观察值之间的联系。
    • 在这种情况下,geom_point 是不是更容易可视化? ggplot(d, aes(x = Index, y = Adjusted, col = as.factor(c))) + geom_point() + scale_color_manual(values = c("green", "red", "blue"))跨度>
    • 谢谢你!这可能是一个很好的解决方案,但是有一种方法可以使用线而不是点?
    【解决方案3】:

    通过这种方法,我找到了集群值发生变化的行,然后选择了这些日期并添加了 NA 值进行调整。出现 NA 值时 geom_line 会中断,从而给出所需的结果。

    library(ggplot2)
    library(dplyr)
    library(data.table)
    
    #add date as a column
    datum <- index(d)
    d <- as.data.table(d)
    d$Datum <- datum
    
    #add row number as a column
    d$Row <- 1:nrow(d)
    
    #find rows of d where cluster value changes
    rows <- which(d$c != dplyr::lag(d$c))
    rows <- d[Row %in% rows]
    
    #add NA values for Adjusted at the Dates where values change(which breaks the geom_line)
    rows <- rows[, Adjusted := NA]
    
    #merge rows (NA values of Adjusted) with d
    d <- rbind(rows, d)
    
    #create a plot
    ggplot(d, aes(x = Datum, y = Adjusted, col = as.factor(c))) + geom_line() + scale_color_manual(values = c("green", "red", "blue"))
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-29
      • 2014-04-03
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      相关资源
      最近更新 更多