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

arun smartpink111 at yahoo.com
Sat Jan 5 03:45:35 CET 2013



Hi,

For the second part of your question, use ?merge() or ?join() from library(plyr)
dat1<-read.table(text="
2011-02-01 15.29130
2011-02-08 17.60278
2011-02-15 17.99737
2011-02-22 25.43690
",sep="",header=FALSE,stringsAsFactors=FALSE)
dat2<-read.table(text="
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
",sep="",header=FALSE,stringsAsFactors=FALSE)

 merge(dat2,dat1,by="V1",all=TRUE)
 #          V1   V2.x     V2.y
#1  2011-02-01 342.34 15.29130
#2  2011-02-02  68.45       NA
#3  2011-02-03 130.47       NA
#4  2011-02-04 129.86       NA
#5  2011-02-05  81.98       NA
#6  2011-02-06  77.30       NA
#7  2011-02-07  81.38       NA
#8  2011-02-08 139.95 17.60278
#9  2011-02-09 124.40       NA
#10 2011-02-15     NA 17.99737
#11 2011-02-22     NA 25.43690
A.K.





----- Original Message -----
From: Matthijs Daelman <matthijs.daelman at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Friday, January 4, 2013 5:53 PM
Subject: [R] How to plot multiple time series with different time base in same plot?

Hi

I have to time series with a different time base.

The first has only sporadic datapoints:
2011-02-01 15.29130
2011-02-08 17.60278
2011-02-15 17.99737
2011-02-22 25.43690

The other has a daily datapoint:
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
...etc.

In Excel, it is fairly easy to get these two time series in one and the
same scatter plot, with two Y-axes, but how would you do this with R?

Also, I would like to obtain a data frame that contains both variables:
2011-02-01 342.34       15.29130
2011-02-02 68.45         NA
2011-02-03 130.47       NA
2011-02-04 129.86       NA
2011-02-05 81.98         NA
2011-02-06 77.30         NA
2011-02-07 81.38         NA
2011-02-08 139.95       17.60278
2011-02-09 124.40        NA
etc.

I can imagine that the data frame can be obtained using (nested) loops, but
that does not seem very efficient with a large dataset.

Any ideas?

Thanks a lot.

Matthijs Daelman

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list