簡體   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