使用 xts 对象向图中添加点、图例和文本
我开始对股票对(配对交易)进行一些分析,这是我为生成图表而编写的函数(pairs.report - 下面列出)。
我需要在一个图中绘制三个不同的线。我列出的函数可以实现我想要的功能,但如果我想在 x 轴(时间线)上进行精细自定义,则需要做一些工作。实际上,它仅在 x 轴上打印年份(对于 10 年的数据)或月份(对于 6 个月的数据),没有刻度格式。
如果我使用 xts 对象,即如果我使用
plot(xts-object-with-date-asset1-asset2, ...)
而不是
plot(date, asset2, ...)
我会立即得到一个格式良好的 x 轴(以及网格和框),但随后使用点()、文本()等函数添加到绘图中,lines() 失败。我想points.xts() 和text.xts() 不会很快出现。
我想要 xts 对象的便利性,但我还需要对我的绘图进行细粒度控制。那么我的工作流程应该是什么样的?我是否必须坚持使用基本图形并手动进行所有自定义?或者有什么办法可以让 xts 为我工作?
我知道lattice和ggplot2,但我现在不想使用它们。这是我提到的功能(欢迎任何改进代码的批评/建议)-
library(xts)
pairs.report <- function(asset1, asset2, dataset) {
#create data structures
attach(dataset)
datasetlm <- lm(formula = asset1 ~ asset2 + 0, data = dataset)
beta = coef(datasetlm)[1]
#add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 4) + 0.1)
# Plot first set of data and draw its axis
ylim <- c(min(asset2,asset1), max(asset2,asset1))
plot(date,
asset2,
axes=T,
ylim=ylim,
xlab="Timeline",
ylab="asset2 and asset1 equity",
type="l",
col="red",
main="Comparison between asset2 and asset1")
lines(date, asset1, col="green")
box()
grid(lwd=3)
# Allow a second plot on the same graph
par(new=T)
# Plot the second plot and
ylim <- c(min(asset1-beta*asset2), max(asset1-beta*asset2))
plot(date,
asset1-beta*asset2,
xlab="", ylab="",
ylim=ylim,
axes=F,
type="l",
col="blue")
#put axis scale on right
axis(side=4,
ylim=ylim,
col="blue",
col.axis="blue")
mtext("Residual Spread",side=4,col="blue",line=2.5)
abline(h=mean(asset1-beta*asset2))
}
I am starting on a bit of analysis on pairs of stocks (pairs trading) and here is the function I wrote for producing a graph (pairs.report - listed below).
I need to plot three different lines in a single plot. The function I have listed does what I want it to do, but it will take a bit of work if I want a fine customisation in the x-axis (the time line). As it is, it prints just the years (for 10 years of data) or the months (for 6 months of data) in the x-axis, with no formatting for ticks.
If I use an xts object, i.e., if I use
plot(xts-object-with-date-asset1-asset2, ...)
instead of
plot(date, asset2, ...)
I get a nicely formatted x-axis right away (along with the grid and the box), but subsequent additions to the plot using functions like points(), text(), lines() fails. I suppose points.xts() and text.xts() are not coming out any time soon.
I would like the convenience of xts objects, but I will also require a fine grained control over my plot. So what should my work-flow be like? Do I have to stick to basic graphics and do all the customisation manually? Or is there a way I can make xts work for me?
I am aware of lattice and ggplot2, but I don't want to use them now. Here is the function I mentioned (any criticism/ suggestions for improvement of the code is welcome) -
library(xts)
pairs.report <- function(asset1, asset2, dataset) {
#create data structures
attach(dataset)
datasetlm <- lm(formula = asset1 ~ asset2 + 0, data = dataset)
beta = coef(datasetlm)[1]
#add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 4) + 0.1)
# Plot first set of data and draw its axis
ylim <- c(min(asset2,asset1), max(asset2,asset1))
plot(date,
asset2,
axes=T,
ylim=ylim,
xlab="Timeline",
ylab="asset2 and asset1 equity",
type="l",
col="red",
main="Comparison between asset2 and asset1")
lines(date, asset1, col="green")
box()
grid(lwd=3)
# Allow a second plot on the same graph
par(new=T)
# Plot the second plot and
ylim <- c(min(asset1-beta*asset2), max(asset1-beta*asset2))
plot(date,
asset1-beta*asset2,
xlab="", ylab="",
ylim=ylim,
axes=F,
type="l",
col="blue")
#put axis scale on right
axis(side=4,
ylim=ylim,
col="blue",
col.axis="blue")
mtext("Residual Spread",side=4,col="blue",line=2.5)
abline(h=mean(asset1-beta*asset2))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
plot.xts
是一个基本绘图函数,这意味着如果您使用了points.default()
和lines.default()
与plot.xts 使用的 x 参数相同。但这是没有必要的。它已经在 xts 和 Zoo 包中进行了哈希处理,因为当加载这些包时,并且您执行方法(行)和方法(点),您会看到这些函数已经可用。points.zoo
记录在 ?plot.zoo 页面上。plot.xts
is a base plot function, which means you can usepoints.default()
andlines.default()
if you used the same x arguments as plot.xts uses. But that is not necessary. It is already hashed out in the xts and zoo packages because when those packages are loaded, and you executemethods(lines)
and methods(points) you see such functions are already available.points.zoo
is documented on the ?plot.zoo page.