[R] datetime data and plotting

Dirk Eddelbuettel edd at debian.org
Fri Oct 17 19:46:17 CEST 2003


On Fri, Oct 17, 2003 at 01:16:00PM -0400, Gabor Grothendieck wrote:
> 
> 
> The problem is related to time zones.  The easiest way to
> handle this is to avoid using POSIXt and use chron instead
> so you don't have to worry about them.
> 
> require(chron)
> day <- 6:16
> dts <- dates(paste("10", day, "03", sep="/"))
> plot(dts,day)
> abline(v=dts)

I don't think I'd call that easiest. Jacob simply did not specify hour,
minute and second for a display where it mattered (mostly because he only
plotted 3 points, with 300 it would have close to impossible to discern).  

One way to address this would be to give a hour and minute as in 

  dates <- strptime(paste(DAY,MONTH,YEAR,"23:59"),"%d %m %Y %H:%M")

(where I also adjust the format string for the space paste() adds).
The three plot commands can also be combined into

  plot(dates,WEIGHT.KG,
       ylim=c(0,max(WEIGHT.KG)),
       xlab="Date",ylab="Weight (kg)",type="o")
       
Hth, Dirk

 
> ---
> From: Jacob Etches <jetches at iwh.on.ca>
>  
> If I take the following simple data:
> 
> YEAR MONTH DAY WEIGHT.KG
> 2003 10 6 1.2
> 2003 10 12 1.2
> 2003 10 16 1.3
> 
> and format the date data and plot it:
> 
> dates <- strptime(paste(DAY,MONTH,YEAR),"%d%m%Y")
> plot(c(min(dates),max(dates)),c(0,max(WEIGHT.KG)),
> xlab="Date",ylab="Weight (kg)",type="n")
> lines(dates,WEIGHT.KG)
> points(dates,WEIGHT.KG)
> 
> I find that the data points are all plotted at (x-1,y),
> where x is in days. Have I requested this behaviour 
> accidentally? I'm using R-1.8 on OS X.
> 
> Printing the dates object looks correct, and simple 
> manipulations such as max(dates)-min(dates) behave 
> normally.
> 
> Jacob Etches
> 
> Doctoral candidate
> Dept of Public Health Sciences
> University of Toronto
> 
> 
> 
> _______________________________________________
> No banners. No pop-ups. No kidding.
> Introducing My Way - http://www.myway.com
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Those are my principles, and if you don't like them... well, I have others.
                                                -- Groucho Marx




More information about the R-help mailing list