[R] color area between two time-series via polygon()?

From: <Jan.Verbesselt_at_csiro.au>
Date: Mon, 25 Feb 2008 11:44:39 +1100


Hi all,  

I would like to color the area between two time-series. I tried it by using the polygon() function but I keeps drawing lines between beginning and end points.

Is there another more appropriate function or how could I close the polygon at the end en the beginning of the time series (e.g., drawing a straight line)?  

The following doesn't plot a polygon between the two time-series:

z <- ts(matrix(rnorm(200), 100), start=c(1961, 1), frequency=12)

plot(z, plot.type="single", lty=1:2)

xx <- cbind(time(z[,1]),rev(z[,2]))

yy <- cbind(as.vector(z[,1]),rev(as.vector(z[,2])))

polygon(xx,yy, col="gray", border = "red")  

I would like to make it look like this (but then for time series)

n <- 100

xx <- c(0:n, n:0)
yy <- c(c(0,cumsum(stats::rnorm(n))), rev(c(0,cumsum(stats::rnorm(n)))))
plot   (xx, yy, type="n", xlab="Time", ylab="Distance")
polygon(xx, yy, col="gray", border = "red")

 

Thanks for your help,

Jan  

        [[alternative HTML version deleted]]



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. Received on Mon 25 Feb 2008 - 00:47:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 25 Feb 2008 - 02:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive