[R] R-project: plot 2 zoo objects (price series) that have some date mis-matches

Gabor Grothendieck ggrothendieck at gmail.com
Mon Apr 4 00:34:12 CEST 2011


On Sun, Apr 3, 2011 at 1:57 PM, algotr8der <algotr8der at gmail.com> wrote:
> I have 2 zoo objects -
>
> 1) Interest rate spread between 10-YR-US-Treasury and 2-YR-US-Treasury
> (object name = sprd)
>
> 2) S&P 500 index (object name = spy)
>
>
>> str(spy)
> ‘zoo’ series from 1976-06-01 to 2011-03-31
>  Data: num [1:8791] 99.8 100.2 100.1 99.2 98.6 ...
>  Index: Class 'Date'  num [1:8791] 2343 2344 2345 2346 2349 ...
>
>> str(sprd)
> ‘zoo’ series from 1976-06-01 to 2011-03-31
>  Data: num [1:9088] 0.68 0.71 0.7 0.77 0.79 0.79 0.82 0.86 0.83 0.83 ...
>  Index: Class 'Date'  num [1:9088] 2343 2344 2345 2346 2349 ...
>
>
> Since there are NA data points in object 'sprd' I created another object
> that omits "NA". The name of that object is 'sprdtmp'.
>
>
>> str(sprdtmp)
> ‘zoo’ series from 1976-06-01 to 2011-03-31
>  Data: atomic [1:8704] 0.68 0.71 0.7 0.77 0.79 0.79 0.82 0.86 0.83 0.83 ...
>  - attr(*, "na.action")=Class 'omit'  int [1:384] 25 70 95 111 118 128 149
> 190 224 260 ...
>  Index: Class 'Date'  num [1:8704] 2343 2344 2345 2346 2349 ...
>
> I want to plot both time series on the same plot with time/date on the
> x-axis and the axis label quarterly (or monthly). One problem is that the
> sprdtmp and spy objects do not have the same number of data points as there
> are times when the equity markets are closed while the interest rate markets
> are open. For the most part the dates overlap. Would this matter if I try to
> plot both on the same plot? And how would I go about plotting these objects
> in one plot.
>
> The second part of the plot requires that both are on different scales. I
> guess I could take a log of the s&p and plot that along with the rate
> spread. But it would be nice for future reference how I could plot 2 series
> in one plot with 2 difference scales.
>
> I spent all night yesterday and this morning trying various options but I
> cant seem to get this to work. I have read through the ?plot, ?plot.zoo and
> ?axis documentation without any success. I would greatly appreciate if
> anyone can point me in the right direction. Thank you kindly.
>

Try this:

plot(na.approx(cbind(z1, z2)), screen = 1)

and in ?plot.zoo see the multivariate plotting example.


where z1 and z2 are two zoo series.  Omit screen = 1 if you want them
in different panels.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list