使用 quantmod 绘制方形折线图

发布于 2024-12-13 03:21:06 字数 1176 浏览 0 评论 0原文

有没有办法让 quantmod 绘制方形折线图?

我尝试修改我的时间序列,以便每个数据点在下一个数据点之前一秒复制(希望这将近似于一条方形线),但 quantmod 似乎按顺序在 x 轴上数据 &均匀间隔,而不考虑 x 的实际值(即,无论 delta-T 是 1 秒还是 1 分钟,一个点与下一个点之间的水平间隔都是相同的)。

我想我可以将我的时间序列从稀疏时间序列转换为密集时间序列(每秒一个条目,而不是每次值变化一个条目),但这看起来非常笨拙,应该是不必要的。

我正在这样构建我的时间序列:

library(quantmod)

myNumericVector <- c(3,7,2,9,4)
myDateTimeStrings <- paste("2011-10-31", c("5:26:00", "5:26:10", "5:26:40", "5:26:50", "5:27:00"))
myXts <- xts(myNumericVector, order.by=as.POSIXct(myDateTimeStrings))

并像这样绘制图表:

chartSeries(myXts, type="line", show.grid="true", theme=chartTheme("black"))

为了说明我所拥有的与我想要的,结果看起来像下面的蓝线,但我想要更像绿色的东西:

在此处输入图像描述

另外,出于好奇,这里是复制时间序列中的点的代码,以便一个值与next 尽可能小:

mySquareDateTimes <- rep(as.POSIXct(myDateTimeStrings),2)[-1]
mySquareDateTimes[seq(2,8,by=2)] <- mySquareDateTimes[seq(2,8,by=2)] - 1
mySquareXts <- xts(rep(myNumericVector,each=2)[-10], order.by=mySquareDateTimes)
chartSeries(mySquareXts, type="line", show.grid="true", theme=chartTheme("black"))

结果小于 理想的。

Is there a way to get quantmod to draw a square line chart?

I've tried modifying my time series so that each data point is replicated one second before the next datapoint (hoping this would approximate a square line), but quantmod seems to data on the x axis sequentially & evenly spaces without regard to the actually values of x (i.e. the horizontal space between one point an the next is the same whether the delta-T is 1 second or 1 minute).

I suppose I could convert my timeseries from a sparse to a dense one (one entry per second instead of one entry per change in value), but this seems very kludgy and should be unnecessary.

I'm constructing my time series thus:

library(quantmod)

myNumericVector <- c(3,7,2,9,4)
myDateTimeStrings <- paste("2011-10-31", c("5:26:00", "5:26:10", "5:26:40", "5:26:50", "5:27:00"))
myXts <- xts(myNumericVector, order.by=as.POSIXct(myDateTimeStrings))

And drawing the chart like so:

chartSeries(myXts, type="line", show.grid="true", theme=chartTheme("black"))

To illustrate what I have vs. what I want, the result looks like the blue line below but I'd like something more like the green:

enter image description here

Also, for the curious, here is the code that replicates points in the time series such that the gap between one value and the next are as small as possible:

mySquareDateTimes <- rep(as.POSIXct(myDateTimeStrings),2)[-1]
mySquareDateTimes[seq(2,8,by=2)] <- mySquareDateTimes[seq(2,8,by=2)] - 1
mySquareXts <- xts(rep(myNumericVector,each=2)[-10], order.by=mySquareDateTimes)
chartSeries(mySquareXts, type="line", show.grid="true", theme=chartTheme("black"))

The results are less than ideal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-12-20 03:21:06

您想要“step”的 line.type

chartSeries(myXts, line.type="s")

请参阅 ?plot,特别是“参数”部分中 ... 下的“type”(您可能需要“S”而不是“s”)。

You want a line.type of "step":

chartSeries(myXts, line.type="s")

See ?plot, specifically "type" under ... in the Arguments section (you may want "S" instead of "s").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文