# [R] Plot 2 different time series data with 2 y axes

Piyush Kumar kuma0177 at gmail.com
Sun Jun 2 23:16:10 CEST 2013

```Hello

I am new to R and I have been trying to plot 2 different time series with 2
y axes. I have been able to plot it so far with 2 y axis but the x axis is
not shifted. I mean, the x range for data1 from day1 and xrange for data2
is from day2, however my plot overlaps the 2 x-axis. Below is the code with

Rplot_x_over_y = function( df1, df2, left_data_type, right_data_type){

#df1 contains data from day1, and its in POSIX time,
eg: [1] "2013-04-03 00:00:00 UTC" "2013-04-03 00:01:00 UTC"
[3] "2013-04-03 00:02:00 UTC" "2013-04-03 00:03:00 UTC"
[5] "2013-04-03 00:04:00 UTC" "2013-04-03 00:05:00 UTC"

#df2 contains data from day2 and is in posix time
[1] "2013-04-04 00:00:00 UTC" "2013-04-04 00:01:00 UTC"
[3] "2013-04-04 00:02:00 UTC" "2013-04-04 00:03:00 UTC"
[5] "2013-04-04 00:04:00 UTC" "2013-04-04 00:05:00 UTC"

time<-df1[[1]]
time2<-df2[[1]]
y1<-df1[[2]]
y2<-df2[[2]]
xleft_limit_min = min(time)
x_left_limit_max = max(time)
y_left_limit_max = min(time)
xright_limit_max = max(time2)

x_range<-c(xleft_limit, xright_limit)
print(x_range)
par(mar=c(5,4,4,5)+.1)
plot(time ,y1,type="l",col="red", ylab=left_data_type)
par(new=TRUE)
plot(time2, y2,,type="l",col="blue", xaxt="n" , yaxt="n",xlab="",ylab="")
axis(4)
mtext(right_data_type,side=4,line=3)
legend("topright",col=c("red","blue"),lty=1,legend=c(left_data_type,
right_data_type))
grid (10,10, lty = 6, col = "cornsilk2")
}

I would expect the plots to have different starting points for  x values
but it seems its unable to understand the dates are different.  A
screenshot is attached. How can I fix it ?

Thanks
-P
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2013-06-02 at 1.55.49 PM.png
Type: image/png
Size: 262139 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130602/0d2dcaab/attachment.png>
```