# [R] Adding a polygon to a time series plot

Andy Bunn abunn at whrc.org
Fri Dec 10 21:05:44 CET 2004

```Arggh! start and end. Of course. How stupid of me. And I'm not going to
interpolate with smooth.spline, but thanks for the warning. Sloppy language.

Thanks, Andy

> -----Original Message-----
> From: Liaw, Andy [mailto:andy_liaw at merck.com]
> Sent: Friday, December 10, 2004 2:49 PM
> To: 'Andy Bunn'; R-Help
> Subject: RE: [R] Adding a polygon to a time series plot
>
>
> > From: Andy Bunn
> >
> > Preamble: Sorry for being dense.
> >
> > Now that that's done, here are my questions.
> >
> > I want to put a polygon on a plot of a time series. I'm going
> > to add lines
> > from a smooth.spline interpolation and other annotation to
>
> Please note: smooth.spline() is for _smoothing_.  If you really want
> interpolation, you should use spline() or splinefun().
>
> > it. But here's
> > the general idea:
> >
> > # Start code
> >      n <- 121
> >      dat <- rnorm(n)
> >      plot(dat, type="n")
> >      polygon(c(1:n,n:1), c(dat,c(rep(0,n))), col = "gray80",
> > border = NA)
> > # End code
> >
> > What if dat is a time series?
> >
> > # Start code that won't parse.
> >      dat.ts <- ts(rnorm(n), start = 1980, frequency = 12)
> >      plot(dat.ts, type="n")
> >      polygon(...) # How do I specify x and y for the time series?
> >                   # Is it easier to skip the time series and
> > add time as an
> > axis
> > # End code that won't parse
>
> AFAICS, the plot will have the x-axis range equal to the time you
> specified,
> so replace 1 with start(dat.ts) and n with end(dat.ts) ought to work, no?
>
> HTH,
> Andy
>
>
> > Here's a work around that is cumbersome.
> >
> > # Start code
> >      dat.ts <- ts(rnorm(n), start = 1980, frequency = 12)
> >      dat <- as.vector(dat.ts)
> >      plot(1:n, dat, type="n", xaxt = "n", axes = FALSE, xlab
> > = "", ylab =
> > "")
> >      polygon(c(1:n,n:1), c(dat,c(rep(0,n))), col = "gray80",
> > border = NA)
> >      axis(1, at = seq(1, n, by = 12), labels = c(1980:1990))
> > # End code
> >
> > Can I use polygon with a time series object? Or is it too awkward?
> >
> > Thanks, Andy
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>
> ------------------------------------------------------------------
> ------------
> Notice:  This e-mail message, together with any attachments,
> contains information of Merck & Co., Inc. (One Merck Drive,
> Whitehouse Station, New Jersey, USA 08889), and/or its affiliates
> (which may be known outside the United States as Merck Frosst,
> Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be
> confidential, proprietary copyrighted and/or legally privileged.
> It is intended solely for the use of the individual or entity
> named on this message.  If you are not the intended recipient,
> and have received this message in error, please notify us
> immediately by reply e-mail and then delete it from your system.
> ------------------------------------------------------------------
> ------------
>

```