使用 quantmod 绘制方形折线图
有没有办法让 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要“step”的
line.type
:请参阅
?plot
,特别是“参数”部分中...
下的“type”(您可能需要“S”而不是“s”)。You want a
line.type
of "step":See
?plot
, specifically "type" under...
in the Arguments section (you may want "S" instead of "s").