繁体   English   中英

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

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

使用 Quantmod 的 chartSeries 和 chart_Series 函数,我一直试图在一个图表上绘制两个时间序列。 以下代码导致图表输出中出现一条粗绿带。 我知道这是因为我正在绑定它并为每个观察显示两个值。 但是,我无法弄清楚如何分别显示两个时间序列。

如果使用 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)

有迹象表明,使它比使用更容易一点了几包chartSeries从quantmod。

包 rtsplot。 使用基本 R 绘图函数绘制时间序列。 阅读帮助 pdf 了解所有选项。

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

或者如果你想使用 ggplot2,你可以使用 tidyquant。 tidyquant 还向 ggplot2 添加了烛台图表选项。 阅读小插图以获取更多信息:

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会做你所要求的。 逻辑参数multi.panel在 1 个(默认)或 2 个单独的面板中绘制两个时间序列。 查看?plot.xts了解更多详情。 如果您知道时间序列的索引是相同的,则可以使用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