【问题标题】:Set the background color for quantmod::chart_Series设置 quantmod::chart_Series 的背景颜色
【发布时间】:2019-04-26 22:57:45
【问题描述】:

我有一个名为 adjPrices.xtsxts 对象,其中包含 OHLC 股票价格历史记录。函数 quantmod::chartSeries 调用如下时,会将这些数据绘制在黑色背景的图表中:

chartSeries(adjPrices.xts,
            subset = '2014-07-01::2015-07-01',
            type = 'bars',
            name = paste(symbol, 'Adjusted Prices'),
            TA = NULL)

但是,具有相同选项的 quantmod::chart_Series 会创建一个白色背景的图表:

chart_Series(adjPrices.xts,
            subset = '2014-07-01::2015-07-01',
            type = 'bars',
            name = paste(symbol, 'Adjusted Prices'),
            TA = NULL)

我想将第二个绘图的背景颜色更改为黑色,并且我正在遵循this answer 中建议的方法。 chart_theme() 的颜色属性为

> chart_theme()$col
$`bg`
[1] "#FFFFFF"

$label.bg
[1] "#F0F0F0"

$grid
[1] "#F0F0F0"

$grid2
[1] "#F5F5F5"

$ticks
[1] "#999999"

$labels
[1] "#333333"

$line.col
[1] "darkorange"

$dn.col
[1] "red"

$up.col
[1] NA

$dn.border
[1] "#333333"

$up.border
[1] "#333333"

这表明我可以将背景颜色设置为黑色,如下所示:

myTheme <- chart_theme()
myTheme$col$`bg` <- "black"
chart_Series(adjPrices.xts,
            subset = '2014-07-01::2015-07-01',
            theme = myTheme,
            type = 'bars',
            name = paste(symbol, 'Adjusted Prices'),
            TA = NULL)

但生成的图表仍然有白色背景: 我还尝试如下定义myTheme

myTheme <- chart_theme()
myTheme$col$`bg` <- "#000000"

但生成的图表仍然有白色背景。

使用chart_Series()时如何设置黑色背景?

【问题讨论】:

    标签: r quantmod


    【解决方案1】:

    目前你不能。 github上有一个尚未解决的旧issue #25。一种解决方法是使用 par 调用 R 图形参数:

    这将创建一个黑色背景。

    par_old <- par(bg = "black")
    chart_Series(SPY)
    par(par_old)
    

    其他一些解决方法正在使用 rtsplot。基于 R 基础图形并可选择绘制蜡烛图。背景颜色通过par设置:

    rtsplot::rtsplot(SPY, type = "candle")
    

    xts 有一个绘图环境 plot.xts,但它不处理蜡烛条。

    Tidyquant 为 ggplot2 提供了 geom_candlestick,但如果您使用 ggplot2 > 3.0,这些功能目前会失败。看到这个tidyquant github issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2010-09-17
      • 2018-07-15
      • 2014-09-04
      • 2010-11-10
      • 2013-08-04
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多