[R] plot two time series with different length and different starting point in one figure.

Yuan, Rebecca rebecca.yuan at bankofamerica.com
Tue Jan 22 20:42:23 CET 2013

```Hello David,

If I use plot with the following code:

plot(A, type = "o", col = plot_colors[plotcolor], axes = FALSE, ann = FALSE)
par(new=TRUE)
plot(B, type = "o", col = plot_colors[plotcolor+1], axes = FALSE, ann = FALSE)
box()

I will have the two series in one plot, but they are only from March,2012 to Nov, 2012, the nonoverlapping months are dropped out...

I know in Matlab that I can specify the x axis such as

Plot(timeofA, A)
Hold on;
Plot(timeofB, B)

to get them in the same figure, but in R, I do not know how to do it.

Thanks,

Rebecca

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: Tuesday, January 22, 2013 2:34 PM
To: Yuan, Rebecca
Cc: R help
Subject: Re: [R] plot two time series with different length and different starting point in one figure.

On Jan 22, 2013, at 7:07 AM, Yuan, Rebecca wrote:

> Hello,
>
> I do have two different time series A and B, they are different in length and starting point. A starts in Jan, 2012 and ends in Dec, 2012 and B starts in March, 2012 and ends in Nov, 2012.
>
> How can I plot those two series A and B in the same plot? I.E., from Jan. 2012 - Feb, 2012, it would have one data point from A and from Mar, 2012-Nov, 2012, it would have two data points from A and B, and in December 2012, it would have one data point from A.

You could set the xlim argument to c( min(timeA, timeB), max(timeA, timeB) ) in the `plot` of either of the series and then use `lines` for the other series, perhaps with a different color argument.

--

David Winsemius
Alameda, CA, USA

----------------------------------------------------------------------
This message, and any attachments, is for the intended r...{{dropped:2}}

```