【问题标题】:Edgy plot of a minute interval time series with POSIXct format %Y-%m-%d %H:%M:%S具有 POSIXct 格式 %Y-%m-%d %H:%M:%S 的分钟间隔时间序列的前卫图
【发布时间】: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")

Edgy plot

#This gives the smooth line when the x-axis is of index form: 
plot(stock_daily$open, type = "l")

Smooth plot

我尝试将数据 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=5minimal reproducible example
  • @Peter 嗨,Peter 非常感谢!很抱歉给您带来不便,因为我对堆栈溢出真的很陌生,而且我不知道如何在这里添加我的数据。让我先解决这个问题,然后再回复您!
  • @Peter 嗨,Peter 我采纳了您的建议,使用 dput 并添加了数据样本。如果您需要任何东西,请告诉我!谢谢!

标签: r ggplot2 plot time-series posixct


【解决方案1】:

我找到了问题的根源。数据不像时间那样连续,例如周末没有数据。 plot 或 ggplot 函数连接每个数据点之间的线,但这些点的间距不相等,因此我们在前卫图中看到了那些长线。解决方案是使用其他绘图函数,例如专为金融数据建模而设计的 chartSeries()。或者我们可以将时间序列转换为数据框,其中时间索引保存为单独的列。这样我们就可以绘制数据,并将时间列添加为 x 标签。

关于类似问题还有另一篇文章: ggplot: Plotting timeseries data with missing values

【讨论】:

    【解决方案2】:

    有趣的是,您加载了 ggplot2 并使用 plot 结束。

    这是你在 ggplot 中的前卫情节

    ggplot(stock_daily, aes(x=timestamp, y = open)) + geom_line()
    

    对同一时间戳使用平滑器:

    ggplot(stock_daily, aes(x=timestamp, y = open)) + geom_smooth()
    

    【讨论】:

    • 嗨,亚当感谢您的快速回复!我使用情节只是为了让事情简单直接地解决问题。不幸的是,我不能使用 geom_smooth,因为我需要查看原始股票价格,稍后我会在上面添加其他图。你能教我如何获得我粘贴在问题中的“平滑情节”吗?非常感谢!
    • 哦,如果我绘制每日价格,则不会出现此问题。您可以更改 av_fun = "TIME_SERIES_DAILY" 并删除 interval=intv 以获取每日价格。
    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2017-06-04
    • 2019-01-28
    • 2020-05-09
    相关资源
    最近更新 更多