[R] axis.POSIXct: Datetime data and plotting
Gabor Grothendieck
ggrothendieck at myway.com
Thu May 27 20:23:05 CEST 2004
Use the Date class rather than POSIXct since Date does not have
time zones in the first place:
d <- as.Date(c("2003/1/15","2003/1/16","2003/1/17","2003/1/18","2003/1/19"))
plot(d,1:length(d),axes=F);box()
axis(2)
abline(v=d[3])
axis.Date(1,d,format=f)
axis.Date(3,at=d,format=f)
Barrowman, Nick <NBarrowman <at> cheo.on.ca> writes:
:
: 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
:
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
:
:
More information about the R-help
mailing list