6

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))
}
Soumendra
  • 1,174
  • 1
  • 15
  • 28
  • maybe my answer to my own question in this thread helps you as well: http://stackoverflow.com/questions/7009711/how-to-get-years-from-a-time-series-index-when-the-underlying-time-series-is-of-m at least you should get other timespans than only 10 year spans. Of course the same works with ticks as well. – Matt Bannert Aug 11 '11 at 08:14
  • 2
    `plot.zoo` works with xts objects and gives you much finer control. `?plot.zoo` has tons of examples. – Joshua Ulrich Aug 11 '11 at 10:34

1 Answers1

4

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • lines.default, while it executed, did not do anything to the plot. I used plot(petroxts) first to plot the first timeseries in that xts object, and after par(new=T) when I execute lines.default(petro$date, petroxts$hpcl, col="green"), the prompt is returned without any messages and nothing happens on the plot. So how do I use lines.default? – Soumendra Aug 16 '11 at 12:34
  • You are probably experiencing the fact that factors and time-dates are represented in ways that may not be apparent to the user. It often happens that plotting functions will do an implicit as.numeric() and the plotting appears not to occur because the points are outside the plot region. More specific answer is not possible given the lack of detail regarding the "dataset" object in your question. If it is a zoo/xts object, then the "dates" may be in the index component of the object. – IRTFM Aug 16 '11 at 14:05