[R] plotting time

Jim Lemon bitwrit at ozemail.com.au
Sun Jul 15 11:36:10 CEST 2001


Greg Trafton wrote:

> Hi, all.  I'm trying to create a plot that has a range of about an
> hour on the x axis, starting at about 10:15...

This may be way too late, but here's a way to do what you want 
(I think).
First, to get the special X axis, use:

plot(...,axes=F,...)

then add the Y axis,

axis(2)

now, you will have to get a list of times in character format for your
X axis.  Here's a function that will give you equally spaced time
intervals 
in the usual format.

time.strings<-function(start,end,increment=1,split=":") {
 if(nargs() >= 3) {
  startlist<-unlist(strsplit(start,split))
  if(length(startlist) == 2) {
   endlist<-unlist(strsplit(end,split))
   if(length(endlist) == 2) {
    starthour<-as.numeric(startlist[1])
    endhour<-as.numeric(endlist[1])
    if(starthour <= endhour) hourlist<-as.character(starthour:endhour)
    else hourlist<-as.character(c(starthour:12,1:endhour))
    startminute<-as.numeric(startlist[2])
    endminute<-as.numeric(endlist[2])
    nhours<-length(hourlist)
    timelist<-vector("character",nhours * 60/increment)
    timeindex<-1
    for(hourindex in 1:nhours) {
     if(hourindex == nhours) maxminute<-endminute
     else maxminute<-59
     while(startminute <= maxminute) {
      timelist[timeindex]<-paste(hourlist[hourindex],
       formatC(startminute,width=2,flag="0"),sep=":")
      startminute<-startminute+increment
      timeindex<-timeindex+1
     }
     startminute<-startminute-60
    }
    timeindex<-timeindex-1
    return(timelist[1:timeindex])
   }
   else cat("end does not seem to be a valid time string\n")
  }
  else cat("start does not seem to be a valid time string\n")
 }
 else cat("Usage: time.strings(start,end,increment)\n")
}

Note that this function will only work for at most 1 day!
(I suppose you could call it once for each day and concatenate.)
You could call this as:

timestrings<-time.strings("10:15","11:15",10)

and finally you can add the X axis

axis(1,at=1:length(timestrings),labels=timestrings)

Hope this helps,

Jim

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list