2012-01-26 102 views
5

當使用plot.xts時,是否有解決方法來設置顏色?在plot.xts中設置顏色

This bug(仍存在於0.8.2中)使得它不可能。我知道我可以使用plot.zoo,但我想知道是否有更清潔的解決方案,因爲該錯誤看起來不會在短時間內得到修復:)

回答

5

這是修補版本。我不得不從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") 

enter image description here

+0

我希望這使得它成爲下一個XTS發佈! –

+1

@DrG,一隻棕色的小鳥告訴我,該功能的作者已經收到警告。 –

0
par(col="blue") 
plot(yourXtsObj) 
box(col="black") 
par(col="black") # reset to default 

與XTS測試v0.9-7

1
plot(yourobject.xts) 
lines(yourobject.xts, col = 'blue')