【问题标题】:Set the color in plot.xts在 plot.xts 中设置颜色
【发布时间】:2012-02-19 11:37:45
【问题描述】:

在使用plot.xts 时是否有解决方法来设置颜色?

This bug(仍然存在于 0.8.2 中)使其成为不可能。我知道我可以使用plot.zoo,但我想知道是否有更清洁的解决方案,因为该错误看起来不会很快得到修复:)

【问题讨论】:

    标签: r graphics xts


    【解决方案1】:

    这是一个补丁版本。我必须从 xts 导出 is.OHLC 才能使其工作。我希望没有副作用。我添加了一个参数 col 并将其显式传递给 plot

    plot.xts2 <- function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto", 
        minor.ticks = TRUE, major.format = TRUE, bar.col = "grey", 
        candle.col = "white", ann = TRUE, axes = TRUE, col = "black", ...) 
    {
        series.title <- deparse(substitute(x))
        ep <- axTicksByTime(x, major.ticks, format = major.format)
        otype <- type
        if (xts:::is.OHLC(x) && type %in% c("candles", "bars")) {
            x <- x[, xts:::has.OHLC(x, TRUE)]
            xycoords <- list(x = .index(x), y = seq(min(x), max(x), 
                    length.out = NROW(x)))
            type <- "n"
        }
        else {
            if (NCOL(x) > 1) 
                warning("only the univariate series will be plotted")
            if (is.null(y)) 
                xycoords <- xy.coords(.index(x), x[, 1])
        }
        plot(xycoords$x, xycoords$y, type = type, axes = FALSE, ann = FALSE, 
            col = col, ...)
        if (auto.grid) {
            abline(v = xycoords$x[ep], col = "grey", lty = 4)
            grid(NA, NULL)
        }
        if (xts:::is.OHLC(x) && otype == "candles") 
            plot.ohlc.candles(x, bar.col = bar.col, candle.col = candle.col, 
                ...)
        dots <- list(...)
        if (axes) {
            if (minor.ticks) 
                axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", 
                    ...)
            axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, 
                lwd = 1, mgp = c(3, 2, 0), ...)
            axis(2, ...)
        }
        box()
        if (!"main" %in% names(dots)) 
            title(main = series.title)
        do.call("title", list(...))
        assign(".plot.xts", recordPlot(), .GlobalEnv)
    }
    
    plot.xts2(as.xts(sample_matrix[,1]), col = "blue")
    

    【讨论】:

    • 我希望它能够进入下一个 xts 版本!
    • @DrG,一只棕色的小鸟告诉我,这个函数的作者已经收到了这个问题的警告。
    【解决方案2】:
    plot(yourobject.xts)
    lines(yourobject.xts, col = 'blue')
    

    【讨论】:

      【解决方案3】:
      par(col="blue")
      plot(yourXtsObj)
      box(col="black")
      par(col="black")  # reset to default
      

      使用 xts v0.9-7 测试

      【讨论】:

        猜你喜欢
        • 2021-08-25
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多