[R] Plot two zoo object with different indexes

Gabor Grothendieck ggrothendieck at gmail.com
Tue Nov 23 19:05:38 CET 2010


On Tue, Nov 23, 2010 at 12:53 PM, Manta <mantino84 at libero.it> wrote:
>
> Dear R community, I have the following two zoo objects:
>
> MONTHLY CPI
>
>> plot(z)
>> par("usr")
> [1] 1977.76333 2011.15333   70.39856  227.03744
>> z=zooreg(cpius$Value,as.yearmon("1979-11"),frequency=12)
>> str(z)
> ‘zooreg’ series from Nov 1979 to Oct 2010
>  Data: num [1:372] 76.2 77 77.8 78.5 79.5 80.3 81.1 82 82 82.6 ...
>  Index: Class 'yearmon'  num [1:372] 1980 1980 1980 1980 1980 ...
>  Frequency: 12
>
> AND A DAILY SERIES IN THE SAME RANGE
>
>> plot(y)
>> par("usr")
> [1]  3233.80000 15362.20000    70.42363   226.38559
>> str(y)
> ‘zoo’ series from 1980-02-01 to 2010-10-31
>  Data: num [1:11231] 76.2 76.2 76.3 76.3 76.3 ...
>  Index: Class 'Date'  num [1:11231] 3683 3684 3685 3686 3687 ...
>
> Now, due to a different index (and a different "usr" parameter for the
> graphic) I am not able to plot the two series together. How shall I do?
>

Try this:

library(zoo)
z1 <- zooreg(1:20, start = as.yearmon("2000-01"), freq = 12)
z2 <- zoo(1:600, as.Date("2000-01-01") + 0:599)
m <- merge(day = aggregate(z1, as.Date, identity), month = z2)
plot(na.approx(m))


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