Gary Lewis
2009-Aug-01 14:26 UTC
[R] xyplot: superpose 2 time series with different time intervals
I could use some advice regarding xyplot. I've got 2 time series. Both cover approximately the same period of time (ie, 1940 to 2009). But one series has annual data and the other has monthly data. One refers to university enrollment; the other to unemployment rates. Both are currently in the same data frame. I'd like to use the monthly times series as a light grayscale background for a plot of the annual time series, showing both series as type "l" (line). Naturally with all the NA's in the annual series, that plot disappears because points are not connected across missing values. I suppose I could make both series annual, but a lot of interesting detail would get lost this way. Or I guess I could interpolate values in the annual series with monthly approximations, but this means 11 out of every 12 values is an approximation. Or I suppose I could plot each series separately and then print them with position information, which I'm reluctant to do because panel.superpose so nicely handles the alignment of the 2 panels. What I'd really like to do is plot each independently but still superposed. Effectively this seems to mean monthly data intervals but line connections across the NA's in the series with annual intervals. Any suggestions would be appreciated. Thanks. Gary Lewis
Gabor Grothendieck
2009-Aug-01 14:37 UTC
[R] xyplot: superpose 2 time series with different time intervals
Try this using the same ts.sim and ts.sim2 from my previous post. https://stat.ethz.ch/pipermail/r-help/2009-August/206697.html library(zoo) library(lattice) plot(na.approx(cbind(as.zoo(ts.sim), as.zoo(ts.sim2))), screen = 1, col = c("black", grey(0.5))) On Sat, Aug 1, 2009 at 10:26 AM, Gary Lewis<gary.m.lewis at gmail.com> wrote:> I could use some advice regarding xyplot. > > I've got 2 time series. Both cover approximately the same period of > time (ie, 1940 to 2009). But one series has annual data and the other > has monthly data. One refers to university enrollment; the other to > unemployment rates. Both are currently in the same data frame. > > I'd like to use the monthly times series as a light grayscale > background for a plot of the annual time series, showing both series > as type "l" (line). Naturally with all the NA's in the annual series, > that plot disappears because points are not connected across missing > values. > > I suppose I could make both series annual, but a lot of interesting > detail would get lost this way. Or I guess I could interpolate values > in the annual series with monthly approximations, but this means 11 > out of every 12 values is an approximation. Or I suppose I could plot > each series separately and then print them with position information, > which I'm reluctant to do because panel.superpose so nicely handles > the alignment of the 2 panels. > > What I'd really like to do is plot each independently but still > superposed. Effectively this seems to mean monthly data intervals but > line connections across the NA's in the series with annual intervals. > > Any suggestions would be appreciated. Thanks. > > Gary Lewis > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Deepayan Sarkar
2009-Aug-01 17:17 UTC
[R] xyplot: superpose 2 time series with different time intervals
On Sat, Aug 1, 2009 at 7:26 AM, Gary Lewis<gary.m.lewis at gmail.com> wrote:> I could use some advice regarding xyplot. > > I've got 2 time series. Both cover approximately the same period of > time (ie, 1940 to 2009). But one series has annual data and the other > has monthly data. One refers to university enrollment; the other to > unemployment rates. Both are currently in the same data frame. > > I'd like to use the monthly times series as a light grayscale > background for a plot of the annual time series, showing both series > as type "l" (line). Naturally with all the NA's in the annual series, > that plot disappears because points are not connected across missing > values.You could define a small wrapper function that discards NA's before drawing lines: my.panel.lines <- function(x, y, ...) { keep <- !is.na(y) panel.lines(x[keep], y[keep], ...) } and use it as a custom panel.groups function: xyplot(<whatever you had before>, panel = panel.superpose, panel.groups = my.panel.lines) -Deepayan> I suppose I could make both series annual, but a lot of interesting > detail would get lost this way. Or I guess I could interpolate values > in the annual series with monthly approximations, but this means 11 > out of every 12 values is an approximation. Or I suppose I could plot > each series separately and then print them with position information, > which I'm reluctant to do because panel.superpose so nicely handles > the alignment of the 2 panels. > > What I'd really like to do is plot each independently but still > superposed. Effectively this seems to mean monthly data intervals but > line connections across the NA's in the series with annual intervals. > > Any suggestions would be appreciated. Thanks. > > Gary Lewis > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >