问题:在一个图表中绘制两个时间序列的问题。

huangapple go评论90阅读模式
英文:

Issue with plotting two time series in one graph

问题

我有一个绘制两个时间序列在一个图中的问题。我已经从Yahoo! Finance下载了两个时间序列。数据没问题。我可以分别绘制它们,但是当我想要将它们绘制在一起以比较它们的动态时,R没有创建我需要的图。我使用了https://rpubs.com/odenipinedo/visualizing-time-series-data-in-R 中的脚本,但没有帮助。然而,该网站上显示的图形就是我需要的,也是R应该完成的工作。

我个人认为问题出在索引上,即日期的位置。由于图形需要x和y,其中y是时间序列数据,x应该是日期(例如2022-02-20),但R没有匹配图形。

  1. lapply(c("quantmod", "dplyr", "tidyr", "timeSeries", "ggplot2"), require, character.only = TRUE)
  2. tickers <- c("TSN", "^GSPC")
  3. start_date <- "2022-02-20"
  4. portfolioPrices <- NULL
  5. for (Ticker in tickers)
  6. portfolioPrices <- cbind(portfolioPrices,
  7. getSymbols(Ticker, from = start_date, src = "yahoo", auto.assign=FALSE)[,4])
  8. portfolioPrices <- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),]
  9. colnames(portfolioPrices) <- tickers
  10. portfolioReturns <- ROC(portfolioPrices, type = "discrete")
  11. portfolioReturns <- as.timeSeries(portfolioPrices)
  12. plot(portfolioReturns$TSN)
  13. lines(portfolioReturns$`^GSPC`, col = "red")
  14. axis(side = 4, at = pretty(portfolioReturns$`^GSPC`)

我已经查看了几个网站和YouTube视频,但没有帮助。我尝试了ggplot和ggseas包,但它们也没有帮助。结果应该类似于这个截图:图片描述

英文:

I have an issue with plotting two time series in one graph. I have downloaded two time series from Yahoo! Finance. Data is OK. I can plot them separately well, but when I want to plot them together to compare their dynamics, R did not create plot I needed. I used scripts from https://rpubs.com/odenipinedo/visualizing-time-series-data-in-R but it did not help. However, the plot that is shown on this website is what I need and what should be done by R.

I personally think the problem is with index, where the dates are located. As graph needs x and y, where y are time series data, x should be dates (e.g. 2022-02-20), but R did not match graphs.

  1. lapply(c(&quot;quantmod&quot;, &quot;dplyr&quot;, &quot;tidyr&quot;, &quot;timeSeries&quot;, &quot;ggplot2&quot;), require, character.only = TRUE)
  2. tickers &lt;- c(&quot;TSN&quot;, &quot;^GSPC&quot;)
  3. start_date &lt;- &quot;2022-02-20&quot;
  4. portfolioPrices &lt;- NULL
  5. for (Ticker in tickers)
  6. portfolioPrices &lt;- cbind(portfolioPrices,
  7. getSymbols(Ticker, from = start_date, src = &quot;yahoo&quot;, auto.assign=FALSE)[,4])
  8. portfolioPrices &lt;- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),]
  9. colnames(portfolioPrices) &lt;- tickers
  10. portfolioReturns &lt;- ROC(portfolioPrices, type = &quot;discrete&quot;)
  11. portfolioReturns &lt;-as.timeSeries(portfolioPrices)
  12. plot(portfolioReturns$TSN)
  13. lines(portfolioReturns$`^GSPC`, col = &quot;red&quot;)
  14. axis(side = 4, at = pretty(portfolioReturns$`^GSPC`)

enter image description here))

I have checked several websites and youtube videos, but nothing helped. I tried ggplot and ggseas packages, but they did not help either. The result should be similar to this screenshot:enter image description here

答案1

得分: 1

以下是代码部分的翻译:

  1. 返回图表只是一个调用autoplot的操作,再加上一些元素的重新排列、颜色、标题和背景面板;
  2. 价格图是一个双Y轴图。右边的Y轴的刻度是根据数据计算的一个因子来缩放。
  1. suppressPackageStartupMessages({
  2. library(quantmod)
  3. library(dplyr)
  4. library(ggplot2)
  5. })
  6. tickers <- c("TSN", "^GSPC")
  7. start_date <- "2022-02-20"
  8. portfolioPrices <- NULL
  9. for (Ticker in tickers) {
  10. portfolioPrices <- cbind(portfolioPrices,
  11. getSymbols(Ticker, from = start_date, src = "yahoo", auto.assign=FALSE)[,4])
  12. }
  13. head(portfolioPrices)
  14. #> TSN.Close GSPC.Close
  15. #> 2022-02-22 92.44 4304.76
  16. #> 2022-02-23 91.99 4225.50
  17. #> 2022-02-24 90.33 4288.70
  18. #> 2022-02-25 93.37 4384.65
  19. #> 2022-02-28 92.66 4373.94
  20. #> 2022-03-01 93.83 4306.26
  21. portfolioPrices <- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),]
  22. colnames(portfolioPrices) <- c("TSN", "GSPC")
  23. portfolioReturns <- ROC(portfolioPrices, type = "discrete")
  24. autoplot(portfolioReturns, facets = NULL) +
  25. scale_color_manual(values = c(TSN = "black", GSPC = "red")) +
  26. ggtitle("Stock Returns") +
  27. theme_bw()
  28. #> Warning: Removed 2 rows containing missing values (`geom_line()`).
  1. Prices <- fortify(portfolioPrices)
  2. (fac <- with(Prices, range(TSN)/range(GSPC)))
  3. #> [1] 0.01676810 0.02124536
  4. fac <- max(fac)
  5. ggplot(data = Prices, aes(Index, TSN)) +
  6. geom_line(aes(color = "TSN")) +
  7. geom_line(aes(y = GSPC * fac, color = "GSPC")) +
  8. scale_x_date(date_breaks = "1 year", date_labels = "%b %Y") +
  9. scale_color_manual(values = c(TSN = "red", GSPC = "black")) +
  10. scale_y_continuous(sec.axis = sec_axis(~ . / fac, name = "GSPC Close Prices")) +
  11. labs(x = "Date", y = "TSN Close Price", color = "") +
  12. ggtitle("Stock Price") +
  13. theme_bw()

创建于2023-02-23,使用 reprex v2.0.2

英文:

Here are two plots.

  1. The returns plot is simply a call to autoplot plus rearrange some elements, color, title, background panel;
  2. the prices plot is a two y axis plot. The right hand y axis is scaled by a factor computed from the data.
  1. suppressPackageStartupMessages({
  2. library(quantmod)
  3. library(dplyr)
  4. library(ggplot2)
  5. })
  6. tickers &lt;- c(&quot;TSN&quot;, &quot;^GSPC&quot;)
  7. start_date &lt;- &quot;2022-02-20&quot;
  8. portfolioPrices &lt;- NULL
  9. for (Ticker in tickers) {
  10. portfolioPrices &lt;- cbind(portfolioPrices,
  11. getSymbols(Ticker, from = start_date, src = &quot;yahoo&quot;, auto.assign=FALSE)[,4])
  12. }
  13. head(portfolioPrices)
  14. #&gt; TSN.Close GSPC.Close
  15. #&gt; 2022-02-22 92.44 4304.76
  16. #&gt; 2022-02-23 91.99 4225.50
  17. #&gt; 2022-02-24 90.33 4288.70
  18. #&gt; 2022-02-25 93.37 4384.65
  19. #&gt; 2022-02-28 92.66 4373.94
  20. #&gt; 2022-03-01 93.83 4306.26
  21. portfolioPrices &lt;- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),]
  22. colnames(portfolioPrices) &lt;- c(&quot;TSN&quot;, &quot;GSPC&quot;)
  23. portfolioReturns &lt;- ROC(portfolioPrices, type = &quot;discrete&quot;)
  24. autoplot(portfolioReturns, facets = NULL) +
  25. scale_color_manual(values = c(TSN = &quot;black&quot;, GSPC = &quot;red&quot;)) +
  26. ggtitle(&quot;Stock Returns&quot;) +
  27. theme_bw()
  28. #&gt; Warning: Removed 2 rows containing missing values (`geom_line()`).

问题:在一个图表中绘制两个时间序列的问题。<!-- -->

  1. Prices &lt;- fortify(portfolioPrices)
  2. (fac &lt;- with(Prices, range(TSN)/range(GSPC)))
  3. #&gt; [1] 0.01676810 0.02124536
  4. fac &lt;- max(fac)
  5. ggplot(data = Prices, aes(Index, TSN)) +
  6. geom_line(aes(color = &quot;TSN&quot;)) +
  7. geom_line(aes(y = GSPC * fac, color = &quot;GSPC&quot;)) +
  8. scale_x_date(date_breaks = &quot;1 year&quot;, date_labels = &quot;%b %Y&quot;) +
  9. scale_color_manual(values = c(TSN = &quot;red&quot;, GSPC = &quot;black&quot;)) +
  10. scale_y_continuous(sec.axis = sec_axis(~ . / fac, name = &quot;GSPC Close Prices&quot;)) +
  11. labs(x = &quot;Date&quot;, y = &quot;TSN Close Price&quot;, color = &quot;&quot;) +
  12. ggtitle(&quot;Stock Price&quot;) +
  13. theme_bw()

问题:在一个图表中绘制两个时间序列的问题。<!-- -->

<sup>Created on 2023-02-23 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年2月23日 19:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定