Skip to content
Prev 314666 / 398502 Next

How to plot multiple time series with different time base in same plot?

On Fri, Jan 4, 2013 at 5:53 PM, Matthijs Daelman
<matthijs.daelman at gmail.com> wrote:
Try this:


data1 <- "2011-02-01 15.29130
 2011-02-08 17.60278
 2011-02-15 17.99737
 2011-02-22 25.43690"

data2 <- "2011-02-01 342.34
 2011-02-02 68.45
 2011-02-03 130.47
 2011-02-04 129.86
 2011-02-05 81.98
 2011-02-06 77.30
 2011-02-07 81.38
 2011-02-08 139.95
 2011-02-09 124.40"

library(zoo)
# read into zoo objects
z1 <- read.zoo(text = data1)
z2 <- read.zoo(text = data2)

# set up
plot(na.aggregate(merge(z1, zoo(, time(z2)))), type = "n", ylab = "")

# plot first series
lines(z1)

# plot 2nd series in red with red Y axis
par(new = TRUE)
plot(z2, col = "red", xaxt = "n", yaxt = "n", ylab = "")
axis(4, col = "red")

See ?plot.zoo for more examples.



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com