简体   繁体   English

R quantmod chartSeries:向图表添加两个时间序列

[英]R quantmod chartSeries: Add two time series to chart

Using Quantmod's chartSeries and chart_Series functions, I've been attempting to chart two time series on a single chart.使用 Quantmod 的 chartSeries 和 chart_Series 函数,我一直试图在一个图表上绘制两个时间序列。 The following code has resulted in a thick green band in the chart output.以下代码导致图表输出中出现一条粗绿带。 I understand that this is because I'm binding it and showing two values for every observation.我知道这是因为我正在绑定它并为每个观察显示两个值。 I can't, however, figure out how to show the two time series separately.但是,我无法弄清楚如何分别显示两个时间序列。

If not possible to do this with Quantmod functions, is it possible to do in ggplot?如果使用 Quantmod 函数无法做到这一点,是否可以在 ggplot 中做到这一点?

library(quantmod)

getSymbols(c('VXX','^VIX'), from = "2019-01-01", to = "2020-03-16")

symbols <- c(VIX$VIX.Adjusted,VXX$VXX.Adjusted)

chartSeries(symbols)

There are a few packages that make it a bit easier than using chartSeries from quantmod.有迹象表明,使它比使用更容易一点了几包chartSeries从quantmod。

package rtsplot.包 rtsplot。 Timeseries plotting using base R plot functions.使用基本 R 绘图函数绘制时间序列。 Read the help pdf for all the options.阅读帮助 pdf 了解所有选项。

library(rtsplot)
rtsplot(VIX)
rtsplot.lines(VXX$VXX.Adjusted)

Or if you want to use ggplot2, you can use tidyquant.或者如果你想使用 ggplot2,你可以使用 tidyquant。 tidyquant also adds a candlestick charting options to ggplot2. tidyquant 还向 ggplot2 添加了烛台图表选项。 Read the vignettes for more info:阅读小插图以获取更多信息:

library(tidyquant)
library(ggplot2)
vols <- tq_get(c('VXX','^VIX'), from = "2019-01-01", to = "2020-03-16") 

vols %>% 
  ggplot(aes(x = date, y = adjusted,  group = symbol)) +
  geom_line() +
  theme_tq()

plot.xts will do what you asked for. plot.xts会做你所要求的。 The logical argument multi.panel plots both time series in 1 (default) or 2 separate panels.逻辑参数multi.panel在 1 个(默认)或 2 个单独的面板中绘制两个时间序列。 Have a look at ?plot.xts for more details.查看?plot.xts了解更多详情。 You can use cbind if you know that the indices of the time series are the same, but a better and safer approach is to use `merge'.如果您知道时间序列的索引是相同的,则可以使用cbind ,但更好、更安全的方法是使用“合并”。

plot.xts(merge(VXX[,6],VIX[,6],join = 'inner'),main = 'plot 2 series on same panel')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM