Robert
Robert

Reputation: 2231

Set the background color for quantmod::chart_Series

I have an xts object named adjPrices.xts that contains an OHLC stock price history. The function quantmod::chartSeries, when called as follows, plots this data in a chart with a black background:

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

enter image description here

However, quantmod::chart_Series with the same options creates a chart with a white background:

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

enter image description here

I want to change the background color of this second plot to black, and I am following the approach suggested in this answer. The color attributes of chart_theme() are

> 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"

This suggests to me that I can set the background color to black as follows:

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)

But the resulting chart still has a white background: enter image description here I also tried defining myTheme as follows:

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

but the resulting chart still has a white background.

How can I set a black background when using chart_Series()?

Upvotes: 3

Views: 904

Answers (1)

phiver
phiver

Reputation: 23608

At the moment you can't. There is an old not yet resolved issue #25 for this on github. A workaround would be calling on the R graphical parameters with par:

This will create a black background.

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

Some other workarounds are using rtsplot. Based on R base graphics and has on option to draw candle charts. Background colours are set via par:

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

xts has a plotting environment plot.xts, but that one doesn't handle candle bars.

Tidyquant has geom_candlestick for ggplot2, but these fail at the moment if you use ggplot2 > 3.0. See this tidyquant github issue

Upvotes: 1

Related Questions