Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 967
nopeva Avatar asked Oct 22 '25 05:10

nopeva


1 Answers

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

like image 97
Jilber Urbina Avatar answered Oct 23 '25 17:10

Jilber Urbina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!