Jan.Verbesselt at csiro.au
2008-Feb-25 00:44 UTC
[R] color area between two time-series via polygon()?
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]]
I think you have to change one statement in your program: xx <- cbind(time(z[,1]),rev(time(z[,2]))) On Sun, Feb 24, 2008 at 7:44 PM, <Jan.Verbesselt at csiro.au> wrote:> 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? Tell me what you want to do, not how you want to do it.