Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Points, Legends and Text to plots using xts objects

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))
}
like image 213
Soumendra Avatar asked Aug 11 '11 05:08

Soumendra


1 Answers

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.

like image 196
IRTFM Avatar answered Sep 26 '22 01:09

IRTFM