[R] axis.POSIXct: Datetime data and plotting

Barrowman, Nick NBarrowman at cheo.on.ca
Thu May 27 20:10:09 CEST 2004


I've run into a problem with the datetime axis generated by axis.POSIXct.  It appears a similar issue was discussed in October 2003 under the subject line "datetime data and plotting" (see https://stat.ethz.ch/pipermail/r-help/2003-October/039071.html), but I wasn't able to determine whether there is a straightforward solution.

The code below produces a graph with apparently contradictory date labels on the top and bottom axes:

f <- "%Y/%m/%d"
d <- as.POSIXct(strptime(c("2003/1/15","2003/1/16",
  "2003/1/17","2003/1/18","2003/1/19"),format=f))
plot(d,1:length(d),axes=F);box()
axis(2)
abline(v=d[3])
axis.POSIXct(1,d,format=f)
axis.POSIXct(3,at=d,format=f)

On my copy of R (I have appended the version information below my signature), this produces a graph with a vertical line that lines up with "2003/01/17" on the top axis, but not on the bottom axis.

The issue seems to relate to time zones: here's what d looks like on my computer:

> d
[1] "2003-01-15 Eastern Standard Time"
[2] "2003-01-16 Eastern Standard Time"
[3] "2003-01-17 Eastern Standard Time"
[4] "2003-01-18 Eastern Standard Time"
[5] "2003-01-19 Eastern Standard Time"

(Actually I'm on Eastern Daylight Time, but let's ignore that for now.)

Changing the axes to display not only the date but also the time shows what's going on:

f <- "%Y/%m/%d"
f2 <- "%Y-%m-%d %H:%M"
d <- as.POSIXct(strptime(c("2003/1/15","2003/1/16",
  "2003/1/17","2003/1/18","2003/1/19"),format=f))
plot(d,1:length(d),axes=F);box()
axis(2)
abline(v=d[3])
axis.POSIXct(1,d,format=f2)
axis.POSIXct(3,at=d,format=f2)

The bottom axis put the ticks at 19:00 on each date.  This seems an odd time until you note that Eastern Standard Time is 5 hours behind UTC, so 19:00 EST is 00:00 UTC.

But is there any easy way to avoid this problem?  It is confusing and inconvenient that axis.POSIXct(1,d,format=f) and axis.POSIXct(3,at=d,format=f) give seemingly contradictory scales.

Many thanks for any help!

Nick Barrowman, Ph.D.
Chief Biostatistician, Chalmers Research Group
Children's Hospital of Eastern Ontario Research Institute
401 Smyth Road, Ottawa, Ontario, K1H 8L1, Canada
Tel   (613) 737-7600 ext. 3971
Fax   (613) 738-4800
Email nbarrowman at cheo.on.ca
URL   www.chalmersresearch.com


> version
         _              
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    9.0            
year     2004           
month    04             
day      12             
language R




More information about the R-help mailing list