plot multiple xts objects superimposed with different scales using both vertical axes

4.6k Views Asked by At

I have two univariate time series that would like to plot in the same screen chart. The problem is that they have very different scales and therefore the chart becomes very difficult to interpret. How can I plot each series superimposed but each of them using a different vertical axis?

library(xts)
mytime <- as.POSIXlt(seq(Sys.time()-100*60+1,Sys.time(),by=60), origin= '1970-01-01')
x <- xts(rnorm(1:100),mytime)
y <- xts(rnorm(1:100,100,10),mytime)
plot(as.zoo( merge(x,y)), screens=1)
1

There are 1 best solutions below

0
On BEST ANSWER

I'm not so sure this is what you want, but here's an idea:

plot(as.zoo(x), las=1)
par(new=TRUE)               
plot(as.zoo(y),
     col=2,
     bty='n',               
     xaxt="n",               
     yaxt="n",              
     xlab="", ylab="")

axis(4, las=1)

legend("topleft",           
       legend=c("x","y"), 
       col=1:2,
       lty=1,              
       cex=0.85) 

enter image description here