[R] Multiple series plot with different 'type' argument
edd at debian.org
Sun Jul 24 19:03:26 CEST 2005
On 24 July 2005 at 12:27, Ruben Roa wrote:
| I need to plot two time series in the same plot and
| they cover the same time range and have the same
| frequency. With
| RSiteSearch("multiple series plot")
| i found this post by Gabor Grothendieck:
| Exactly what i need except for one detail. I want one series
| to be made of points and the other by a line. When I simply
| xts <- ts(x$b,start=x$a)
| yts <- ts(y$b,start=y$a)
| or if i use the 'type' argument inside gpars=list()
| i get an error of "invalid plot type".
| Then if i try
| i get warnings about NAs introduced by coercion and the
| plot still shows two lines.
| Is there any other way i can get these two time series
| in the same plot one with points and the other with lines?
| Any help much appreciated.
Yes, you can, but it requires a little bit of tinkering. Here is a complete
example with two random series. I prefer zoo as a container over ts(). You
can easily change the plotting styles to get points instead of lines etc pp.
## make up an date index
index <- Sys.Date() + seq(-99,0,by=1)
## init random number generato
## and create two random series, with the date index
X1 <- zoo(cumsum(rnorm(100)), index)
X2 <- zoo(cumsum(rnorm(100)), index)
## plot series, suppress axes, set limit to range of both series
oldpar <- par(mar=c(4,4,2,4)) # extra space on the right
plot(X1, col='blue', ylab="X1", type='l', xaxs="i", ylim=range(X, Y))
axis(2, col.axis='blue') # y-axis for X1, overplotting
grid() # prettier
par(new=TRUE) # add to the plot
plot(X2, col='green', type='l', ylab="", axes=FALSE, xaxs="i", ylim=range(X, Y))
## need mtext() to annotate 2nd y-axis as title() doesn't do it
mtext("X2", side=4, line=2)
Hope this helps, Dirk
Statistics: The (futile) attempt to offer certainty about uncertainty.
-- Roger Koenker, 'Dictionary of Received Ideas of Statistics'
More information about the R-help