Re: [R] Multiple series plot with different 'type' argument

From: Dirk Eddelbuettel <edd_at_debian.org>
Date: Mon 25 Jul 2005 - 03:03:26 EST

On 24 July 2005 at 12:27, Ruben Roa wrote:
| Hi:
| 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:
| http://finzi.psych.upenn.edu/R/Rhelp02a/archive/42281.html
| 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
| try:
| xts <- ts(x$b,start=x$a[1])
| yts <- ts(y$b,start=y$a[1])
| ts.plot(xts,yts,type=c("p","l"))
| or if i use the 'type' argument inside gpars=list()
| i get an error of "invalid plot type".
| Then if i try
| ts.plot(xts,yts,type1="p",type2="l")
| 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.

library(zoo)

## make up an date index
index <- Sys.Date() + seq(-99,0,by=1)

## init random number generato
set.seed(42)
## 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)) axis(4, col.axis='green')
## 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'

______________________________________________
R-help@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
Received on Mon Jul 25 03:10:28 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:59 EST