[R] plotting multiple series in one plot

Gabor Grothendieck ggrothendieck at myway.com
Fri Nov 26 16:43:51 CET 2004


Apoian, Zack <Zack.Apoian <at> sac.com> writes:

: 
: say we have:
: 
: > x<-data.frame(a=c(1,2,3,4,5),b=c(1,1,1.5,2,2))
: > y<-data.frame(a=c(1,2,3,4,5),b=c(1,2,2,3,3))
: 
: How would I plot this so that, with the shared $a as the x-axis values, I
: have both $b columns plotted together?  (a comparison of the two?)

I have converted your data to ts class since you have
described them as time series and they appear to be regularly
spaced. I have modified your example slightly so that time scales
are not the same just to show that the code still works
in that case.  The code does rely on the series being
regularly spaced (if they are irregular see plot.zoo in the zoo
package).

x<-data.frame(a=c(2,3,4,5,6),b=c(1,1,1.5,2,2))
y<-data.frame(a=c(1,2,3,4),b=c(1,2,2,3))

xts <- ts(x$b,start=x$a[1])
yts <- ts(y$b,start=y$a[1])
ts.plot(xts,yts,col=c("red","blue"))




More information about the R-help mailing list