Sou*_*dra 6 graphics r graph xts quantmod
我开始对成对股票(对交易)进行一些分析,这是我为生成图表而编写的函数(pairs.report - 下面列出).
我需要在一个图中绘制三条不同的线.我列出的功能完成了我想要的功能,但如果我想在x轴(时间线)中进行精细定制,则需要一些工作.实际上,它只打印x轴上的年份(10年的数据)或月份(6个月的数据),没有刻度的格式.
如果我使用xts对象,即如果我使用
plot(xts-object-with-date-asset1-asset2, ...)
Run Code Online (Sandbox Code Playgroud)
代替
plot(date, asset2, ...)
Run Code Online (Sandbox Code Playgroud)
我得到一个格式很好的x轴(连同网格和方框),但随后使用点(),text(),lines()等函数添加到图中失败.我想points.xts()和text.xts()不会很快出来.
我想要xts对象的便利,但我还需要对我的情节进行精细控制.那么我的工作流程应该是什么样的呢?我是否必须坚持使用基本图形并手动完成所有自定义操作?或者有什么方法可以让xts为我工作?
我知道格子和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))
}
Run Code Online (Sandbox Code Playgroud)
plot.xts是一个基本绘图函数,这意味着您可以使用points.default()andlines.default()如果您使用与plot.xts 相同的 x 参数。但这是没有必要的。它已经在 xts 和 Zoo 包中进行了哈希处理,因为当加载这些包时,您执行methods(lines)方法(点),您会看到这些函数已经可用。points.zoo记录在 ?plot.zoo 页面上。