[R] xyplot: superpose 2 time series with different time intervals

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Aug 1 19:17:29 CEST 2009

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)


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

More information about the R-help mailing list