Charles Novaes de Santana
2013-Oct-16 21:57 UTC
[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]]
Hi, This may get you started. testtime1 <- factor(testtime,levels=testtime) ?plot(as.numeric(testtime1),var,type="b",xlab="Time",ylab="Var",xaxt="n") ?axis(1,at= as.numeric(testtime1), labels=levels(testtime1)) ## labels are not spaced according to time interval #Another idea would be: testtime2 <- strptime(testtime,"%H:%M:%S") testtime2[testtime2$hour < 20] <- testtime2[testtime2$hour < 20]+ 24*60*60 ?plot(testtime2,var,xaxt="n",type="b") ?axis.POSIXct(1,testtime2,format="%H:%M:%S") A.K. On Wednesday, October 16, 2013 6:00 PM, Charles Novaes de Santana <charles.santana at gmail.com> wrote: 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.
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]]