问题
I am trying to plot two charts on one chartSeries
in quantmod in R. I am having some difficulty doing this.
library(quantmod)
tickers <- c('GLD', 'GDX')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data)
chartSeries(Cl(data$GLD), TA="addTA(Cl(data$GDX), on=1)")
addRSI()
回答1:
You could use chart_Series
instead of chartSeries
.
chart_Series(Cl(data$GLD))
add_TA(Cl(data$GDX), on = 1)
And then if you want RSI below in a sub panel, just add add_RSI()
.
Another approach is to use version >= 0.10.0 of xts
(i.e. don't use quantmod
at all), which you can get from https://github.com/joshuaulrich/xts (0.10.0 is not yet on CRAN). The new plot
function in xts
is very friendly with plotting multiple columns of an xts
object all at once. Check out ?plot.xts
for examples of new functionality.
Edit #2:
To see relative changes more easily, you can normalise your price series in many ways. This is a typical approach (using a 0 origin is what Google charts does):
normalise_series <- function(xdat) xdat / coredata(xdat)[1]
getSymbols("USO")
window <- "2013/"
# Define colour of default chart line to chart_Series in mytheme object
# which is passed to chart_Series:
mytheme <- chart_theme()
mytheme$col$line.col <- "darkgreen"
chart_Series(normalise_series(Cl(data$GLD)[window]) - 1, theme = mytheme)
add_TA(normalise_series(Cl(data$GDX)[window]) - 1, on = 1, col = "red", lty = 3)
add_TA(normalise_series(Cl(USO)[window]) - 1, on = 1, col = "blue", lty =2)
add_TA(RSI(Cl(data$GLD)), on = NA, col = "darkgreen")
add_TA(RSI(Cl(data$GDX)), on = 2, col = "red", lty = 3)
# Or add RSIs on different subpanels to improve readability of charts:
add_TA(RSI(Cl(USO)), on = NA, col = "blue", lty = 2)
来源:https://stackoverflow.com/questions/38136008/adding-multiple-chart-series-in-quantmod-r