使用xts对象将点,图例和文本添加到图中 [英] Adding Points, Legends and Text to plots using xts objects

查看:223
本文介绍了使用xts对象将点,图例和文本添加到图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始对成对股票进行一些分析(对交易),这里是我为生成图形所写的函数(pairs.report-下面列出)。

我需要在一个绘图中绘制三条不同的线。我列出的函数做我想做的事情,但是如果我想在x轴(时间线)中进行良好的自定义,它将需要一些工作。实际上,它只是在X轴上打印年份(10年的数据)或月份(6个月的数据),没有格式化蜱。



如果我使用一个xts对象,也就是说,如果我使用

  plot(xts-object-with-date-asset1 -asset2,...)

而不是

  plot(date,asset2,...)

我得到一个很好格式化的x轴(与网格和框一起),但随后使用诸如points(),text(),lines()等函数添加到绘图中失败。我想points.xts()和text.xts()不会很快出来。



我想要xts对象的方便,但我也会要求对我的情节进行细致的控制。那么我的工作流程应该如何?我是否必须坚持使用基本图形并手动进行所有定制?或者有什么办法可以让xts适合我?

我知道lattice和ggplot2,但我现在不想使用它们。这里是我提到的功能(欢迎任何批评/改进代码的建议) -

  library(xts)

pairs.report< - 函数(asset1,asset2,dataset){

#create数据结构
attach(数据集)
datasetlm< - lm (公式=资产1〜资产2 + 0,数据=数据集)
beta = coef(datasetlm)[1]

#为框架内的绘图右边距增加额外空间
par (mar = c(5,4,4)+ 0.1)

#绘制第一组数据并绘制其轴线
ylim < - c(min(asset2,asset1) ,max(asset2,asset1))
plot(日期,
asset2,
轴= T,
ylim = ylim,
xlab =时间轴,
ylab =asset2 and asset1 equity,
type =l,
col =red,
main =asset2与asset1之间的比较)
lines (date,asset1,col =green)
box()
grid(lwd = 3)

#在同一张图上允许第二个图
par (新= T)

#绘制第二个图和
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)

#右轴缩放
轴(side = 4,
ylim = ylim,
col =blue,
col.axis =blue)
mtext(剩余价差,side = 4,col =blue,line = 2.5)

abline(h =平均值(资产1-beta *资产2))
}


解决方案

plot.xts 是一个底图函数,这意味着您可以使用 points.default() lines.default()如果您使用与plot.xts使用相同的x参数。但这不是必要的。它已经在xt和zoo包中被哈希了,因为当这些包被加载,并且执行方法(行)和方法(点)时,你会看到这些函数已经可用。 <?c $ c> points.zoo 记录在?plot.zoo页面上。


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))
}

解决方案

plot.xts is a base plot function, which means you can use points.default() and lines.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 execute methods(lines) and methods(points) you see such functions are already available. points.zoo is documented on the ?plot.zoo page.

这篇关于使用xts对象将点,图例和文本添加到图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆