【发布时间】:2020-10-16 16:21:44
【问题描述】:
首先感谢您的帮助!我试图绘制一个时间间隔为 5 分钟的股票价格。如果我将其绘制为时间序列,则结果图非常前卫。如果我简单地将价格绘制成一个数字序列,这是正确的。
library(tidyquant)
library(ggplot2)
Symb <- "spot"
intv<- "5min"
stock_daily <- tq_get(Symb, get = "alphavantage", av_fun = "TIME_SERIES_INTRADAY", interval = "5min", outputsize = "full")
这是数据:
> dput(head(stock_daily, 20))
structure(list(symbol = c("spot", "spot", "spot", "spot", "spot",
"spot", "spot", "spot", "spot", "spot", "spot", "spot", "spot",
"spot", "spot", "spot", "spot", "spot", "spot", "spot"), timestamp = structure(c(1591349700,
1591350000, 1591350300, 1591350600, 1591350900, 1591351200, 1591351500,
1591351800, 1591352100, 1591352400, 1591352700, 1591353000, 1591353300,
1591353600, 1591353900, 1591354200, 1591354500, 1591354800, 1591355100,
1591355400), tzone = "UTC", class = c("POSIXct", "POSIXt")),
open = c(179, 179.38, 180.53, 182.5249, 183.1, 183.4, 183.71,
183.76, 182.705, 181.92, 181.87, 181.9626, 181.99, 181.75,
182.29, 181.9, 181.56, 181.309, 181.37, 181.06), high = c(179.78,
181.51, 182.59, 183.48, 183.665, 183.79, 183.99, 184.155,
182.82, 182.19, 181.99, 182.4, 182.23, 182.27, 182.38, 181.91,
181.6, 181.7, 181.51, 181.1673), low = c(177.505, 179.33,
180.3, 182.18, 182.955, 182.83, 183.17, 182.64, 181.71, 181.09,
181.48, 181.62, 181.43, 181.75, 181.9, 181.43, 181.32, 181.2872,
181.02, 180.37), close = c(179.4, 180.6, 182.3, 183.11, 183.4685,
183.68, 183.655, 182.72, 182.08, 181.66, 181.99, 181.77,
181.6277, 182.27, 181.91, 181.6138, 181.51, 181.435, 181.1,
180.37), volume = c(55046, 24842, 20192, 13935, 19356, 19911,
11355, 12081, 11462, 9882, 5826, 8278, 5058, 6790, 4437,
6248, 4489, 9217, 4638, 12602)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
#This gives the edgy line when the x-axis consists of time:
plot(stock_daily$timestamp, stock_daily$open, type = "l")
#This gives the smooth line when the x-axis is of index form:
plot(stock_daily$open, type = "l")
我尝试将数据 stock_daily 转换为 xts 对象,但它不起作用。我怀疑这可能是因为时间间隔不均等,例如数据是在 2020-06-23 11:30:00 提供的,但是下一个是 2020-06-23 11:45:00。它跳过了 11:35:00 和 11:40:00。但是我添加了这两个缺失的行,其值为 NA 并再次尝试,它仍然没有工作。
【问题讨论】:
-
欢迎堆栈溢出。如果您使您的问题具有可重复性,包括一些可用于测试和验证可能解决方案的数据,那么为您提供帮助会更容易。你可以使用
dput(head(your_data, 20))。 - 我尝试运行您的代码,它需要一个“Alpha Vantager API 密钥”。另外,如果您有时间:看看speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5 和minimal reproducible example -
@Peter 嗨,Peter 非常感谢!很抱歉给您带来不便,因为我对堆栈溢出真的很陌生,而且我不知道如何在这里添加我的数据。让我先解决这个问题,然后再回复您!
-
@Peter 嗨,Peter 我采纳了您的建议,使用 dput 并添加了数据样本。如果您需要任何东西,请告诉我!谢谢!
标签: r ggplot2 plot time-series posixct