【问题标题】:How do I set width of candles in candle chart using plot.xts?如何使用 plot.xts 在蜡烛图中设置蜡烛的宽度?
【发布时间】:2016-11-22 16:21:14
【问题描述】:

我在 XTS 中有简单的 OHLC 数据

SF <- structure(c(1.064, 1.07, 1.071, 1.08, 1.08, 1.076, 1.078, 1.08,
1.08, 1.082, 1.081, 1.082, 1.074, 1.07, 1.073, 1.075, 1.081,
1.084, 1.092, 1.091, 1.097, 1.095, 1.099, 1.094, 1.096, 1.097,
1.096, 1.096, 1.097, 1.091, 1.078, 1.083, 1.088, 1.084, 1.081,
1.095, 1.096, 1.085, 1.074, 1.075, 1.073, 1.07, 1.068, 1.072,
1.084, 1.08, 1.081, 1.077, 1.081, 1.083, 1.084, 1.083, 1.082,
1.082, 1.075, 1.074, 1.075, 1.092, 1.086, 1.092, 1.093, 1.098,
1.102, 1.103, 1.099, 1.098, 1.1, 1.101, 1.098, 1.098, 1.1, 1.092,
1.084, 1.087, 1.088, 1.084, 1.096, 1.099, 1.097, 1.086, 1.078,
1.076, 1.076, 1.073, 1.064, 1.069, 1.071, 1.077, 1.075, 1.074,
1.078, 1.078, 1.08, 1.079, 1.078, 1.073, 1.068, 1.07, 1.069,
1.074, 1.08, 1.083, 1.089, 1.09, 1.096, 1.094, 1.092, 1.092,
1.094, 1.094, 1.09, 1.092, 1.088, 1.08, 1.076, 1.078, 1.081,
1.079, 1.08, 1.09, 1.084, 1.072, 1.073, 1.069, 1.066, 1.07, 1.067,
1.072, 1.08, 1.079, 1.076, 1.077, 1.08, 1.08, 1.082, 1.081, 1.081,
1.074, 1.072, 1.073, 1.074, 1.081, 1.084, 1.091, 1.092, 1.097,
1.097, 1.099, 1.095, 1.095, 1.097, 1.097, 1.096, 1.094, 1.091,
1.08, 1.083, 1.086, 1.083, 1.082, 1.095, 1.096, 1.086, 1.074,
1.075, 1.073, 1.071, 1.072), .Dim = c(42L, 4L), .Dimnames = list(
NULL, c("Open", "High", "Low", "Close")), index = structure(c(1353427200,
1353513600, 1353600000, 1353859200, 1353945600, 1354032000, 1354118400,
1354204800, 1354464000, 1354550400, 1354636800, 1354723200, 1354809600,
1355068800, 1355155200, 1355241600, 1355328000, 1355414400, 1355673600,
1355760000, 1355846400, 1355932800, 1356019200, 1356278400, 1356451200,
1356537600, 1356624000, 1356883200, 1357056000, 1357142400, 1357228800,
1357488000, 1357574400, 1357660800, 1357747200, 1357833600, 1358092800,
1358179200, 1358265600, 1358352000, 1358438400, 1358697600), tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"))

plot.xts(SF, type='candles')

蜡烛出来的很细。如何使它们变宽?

通过查看 plot.xts 的源代码,我看到调用了 xts 内部函数 plot.ohlc.candles 以将 OHLC 数据绘制为蜡烛,其中传递了 ...。 H

plot.ohlc.candles(x, bar.col = bar.col, candle.col = candle.col, 
            ...)

但是,如果我尝试在我的 plot.xts 函数调用中设置参数 width=0.5,我会收到宽度不是图形参数的警告

Warning messages:
1: In plot.window(...) : "width" is not a graphical parameter
2: In plot.xy(xy, type, ...) : "width" is not a graphical parameter
3: In axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...) :
  "width" is not a graphical parameter
4: In axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, lwd = 1,  :
  "width" is not a graphical parameter
5: In axis(2, ...) : "width" is not a graphical parameter
6: In title(width = 0.5) : "width" is not a graphical parameter
There were 12 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: "width" is not a graphical parameter
2: "width" is not a graphical parameter
3: "width" is not a graphical parameter
4: "width" is not a graphical parameter
5: "width" is not a graphical parameter
6: "width" is not a graphical parameter
7: "width" is not a graphical parameter
8: "width" is not a graphical parameter
9: "width" is not a graphical parameter
10: "width" is not a graphical parameter
11: "width" is not a graphical parameter
12: "width" is not a graphical parameter

我无法使用 quantmod 或 xtsExtra,因为我试图并排绘制两个不同的图(一个时间序列和其他正常的 XY 线图)。

【问题讨论】:

    标签: r xts


    【解决方案1】:

    您可以忽略警告,但您需要将width 设置为非常大的值。查看plot.ohlc.candles可以看到width是基于xts对象的索引,以秒为单位。

    R> xts:::plot.ohlc.candles
    function (x, width = 0.2, order = 1:4, bar.col = "grey", candle.col = "white", 
        ...) 
    {
        segments(.index(x), x[, order[2]], .index(x), x[, order[3]], 
            col = bar.col, ...)
        rect(.index(x) - width, x[, order[1]], .index(x) + width, 
            x[, order[4]], col = candle.col, ...)
    }
    <environment: namespace:xts>
    

    这会将宽度设置为 1/4 天:

    plot.xts(SF, type='candles', width=60*60*24*0.25)
    

    【讨论】:

    • 我自己也想通了 :) +1 澄清一下 :)
    【解决方案2】:

    正如 Joshua 所说,这是我为获得正确图表所做的工作

    candlecolors <- ifelse(SF[,'Close'] > SF[,'Open'], 'RED', 'GREEN')
    plot.xts(SF, type='candles',  width=25000, candle.col=candlecolors, bar.col='BLACK')
    

    这给了我下面的图表。

    【讨论】:

    • 有没有办法自动调整宽度(=25000?)以避免根据周期范围手动设置?
    • 也不是很好,因为您通常只想显示交易日,而不是周末
    【解决方案3】:

    由于蜡烛只是线条,您可以使用lwd 参数来增加线条宽度。可惜直接使用会报错:

    plot.xts(SF, type='candles',lwd=2)
    Error in axis(1, at = xycoords$x[ep], labels = names(ep), las = 1, lwd = 1,  : 
      formal argument "lwd" matched by multiple actual arguments
    

    这似乎是 plot.xts 包装器工作方式的问题。

    您可以通过使用par 设置它来使其工作:

    opar <- par(lwd=2)
    plot.xts(SF, type='candles')
    par(opar) # reset to original parameters
    

    虽然这也会影响网格线和图表边框。

    【讨论】:

    • 不幸的是,lwd=2 会影响您提到的其他所有内容,导致图表不美观。寻找保留轴、网格线等的东西
    【解决方案4】:

    那么缩放包呢?然后你可以放大和缩小,但是你想通过“绘图”代码末尾的 zm() 像

    plot(datax)
    lines(datay)
    zm()
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2021-12-26
      • 2021-02-28
      相关资源
      最近更新 更多