[R] Plot time series data irregularly hourly-spaced

William Dunlap wdunlap at tibco.com
Thu Oct 17 02:34:29 CEST 2013


You could bump up the day each time an hour was less than the previous one.  E.g.,
  testtime <- c("20:00:00","22:10:00","22:20:00","23:15:00","23:43:00","00:00:00","00:51:00","01:00:00")
  var <- seq_along(testtime) # so you know what the plot should look like
  # turn it ino a POSIXlt object so you can do arithmetic on it
  t <- strptime(testtime,format="%H:%M:%S")
  # now add a day each time t[i]<t[i-1]
  td <- t + .difftime(cumsum(c(FALSE, diff(t)<0)), units="days")
  # compare plots
  par(mfrow=c(2,1))
  plot(t,var,type="b",xlab="Time",ylab="Var")
  plot(td,var,type="b",xlab="Time",ylab="Var")
This is dicey because you may have skipped more than one day.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Law, Jason
> Sent: Wednesday, October 16, 2013 5:04 PM
> To: Charles Novaes de Santana; r-help at r-project.org
> Subject: Re: [R] Plot time series data irregularly hourly-spaced
> 
>  You just need the date, otherwise how would it know what time comes first?  In
> strptime(), a date is being assumed.
> 
> Try this:
> 
> testtime<-
> c("20:00:00","22:10:00","22:20:00","23:15:00","23:43:00","00:00:00","00:51:00","01:00:
> 00")
> testday <- rep(Sys.Date() - c(1,0), times = c(5,3))
> plot(as.POSIXct(paste(testday, testtime)), var)
> 
> Jason
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Charles Novaes de Santana
> Sent: Wednesday, October 16, 2013 2:58 PM
> To: r-help at r-project.org
> Subject: [R] Plot time series data irregularly hourly-spaced
> 
> Dear all,
> 
> I have a time series of data that I would like to represent in a plot. But I am facing some
> problems to do it because the time is represented in "hours", it can start in one day and
> end in another day, and it is not regularly spaced.
> 
> My problem is that when I plot my data, my X-axis always starts from the lower values of
> my time data. For example, I would like to plot data that starts at 20:00:00 and ends at
> 01:00:00, but R considers that 01:00:00 is lower than 21:00:00 and my plot is kind of
> "crossed over time".
> 
> Please try this example to see it graphically:
> 
> testtime<-
> c("20:00:00","22:10:00","22:20:00","23:15:00","23:43:00","00:00:00","00:51:00","01:00:
> 00")
> var<-runif(length(testtime),0,1)
> plot(strptime(testtime,format="%H:%M:%S"),var,type="b",xlab="Time",ylab="Var")
> 
> In this case, I would like to have a plot that starts at 20:00:00 and ends at 01:00:00.
> 
> Does anybody know how to make R understand that 00:00:00 comes after 20:00:00 in
> this case? Or at least does anybody know a tip to make a plot with this kind of X-axis?
> 
> Thanks for your time and thanks in advance for any help.
> 
> Best regards,
> 
> Charles
> --
> Um axé! :)
> 
> --
> Charles Novaes de Santana, PhD
> http://www.imedea.uib-csic.es/~charles
> 
> 	[[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