Matthijs Daelman
2013-Jan-04 22:53 UTC
[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]]
Mario Bourgoin
2013-Jan-05 00:01 UTC
[R] How to plot multiple time series with different time base in same plot?
Hi Matthijs, Look at the code in the attached file and try it out. Best, Mario On Fri, Jan 4, 2013 at 5:53 PM, Matthijs Daelman <matthijs.daelman at gmail.com> wrote:> 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. >-- Mario Bourgoin, Ph.D. Department of Mathematics Brandeis University Waltham, MA
Gabor Grothendieck
2013-Jan-05 00:54 UTC
[R] How to plot multiple time series with different time base in same plot?
On Fri, Jan 4, 2013 at 5:53 PM, Matthijs Daelman <matthijs.daelman at gmail.com> wrote:> 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. >Try this: data1 <- "2011-02-01 15.29130 2011-02-08 17.60278 2011-02-15 17.99737 2011-02-22 25.43690" data2 <- "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" library(zoo) # read into zoo objects z1 <- read.zoo(text = data1) z2 <- read.zoo(text = data2) # set up plot(na.aggregate(merge(z1, zoo(, time(z2)))), type = "n", ylab = "") # plot first series lines(z1) # plot 2nd series in red with red Y axis par(new = TRUE) plot(z2, col = "red", xaxt = "n", yaxt = "n", ylab = "") axis(4, col = "red") See ?plot.zoo for more examples. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
arun
2013-Jan-05 02:45 UTC
[R] How to plot multiple time series with different time base in same plot?
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.
Seemingly Similar Threads
- Does R have an equivalent for Matlab's cell array?
- ggplot2: two time series with different dates in a single plot
- Problem with which function
- how to suppress the intercept in an lm()-like formula method?
- How can I access the title of a table read via read.csv?