[Rd] proposal for new axis.Date/axis.POSIXct

Felix Andrews felix at nfrac.org
Mon Dec 21 13:44:46 CET 2009


Hi R-devel.

I've noticed a couple of quirks in the current time/date axis
functions (axis.Date, axis.POSIXct, and the equivalents in lattice).
Looking at the code, it seems like a fairly ad-hoc approach, often
using pretty() on components of the time. This is not always ideal -
for example a one-hour interval gets cut into 10-minute chunks rather
than the more natural 15-minute chunks (since pretty() doesn't know
about the minutes in an hour, etc). Generally the number of tick marks
produced varies a lot, and there are a couple of strange cases: try
plot(0:1 ~ as.POSIXct(paste(2002:2003,"-02-01",sep="")))

So, I've written a function prettyDate() that attempts to act like
pretty(), but with dates and times. Like pretty, it takes arguments
'n' and 'min.n' which specify the desired and minimum number of ticks,
respectively.

http://pastie.org/751640

By the way, also listed there is an extension of trunc.POSIXt with
extra units "weeks", "months", "years", "decades", "centuries".

Following is a test of prettyDate() for axis labelling, drawn for a
sequence of different time intervals.

source("http://pastie.org/751640.txt")

steps <-
    list("10 secs",
         "1 min", "5 mins", "10 mins", "15 mins", "30 mins",
         "1 hour", "3 hours", "6 hours", "12 hours",
         "1 DSTday", "1 week", "2 weeks",
         "1 month", "3 months", "6 months",
         "1 year", "2 years", "5 years", "10 years",
         "20 years", "50 years", "100 years")
names(steps) <- paste("span =", unlist(steps))

from <- as.POSIXct("2002-02-02 02:02")
devAskNewPage(TRUE)

lapply(steps, function(s) {
    times <- seq(from, by = s, length = 2)
    plot(0:1 ~ times, yaxt = "n", ylab = "")
    x <- mean(par("usr")[1:2])
    text(x, 0.5, paste("span:", s), cex = 2)
    text(x, 0.33, paste(format(times), collapse="\n"))
    text(x, 0.05, "current axis.POSIXct")
    text(x, 0.95, "proposed new prettyDate axis")
    ## draw new proposed axis function at top of plot
    timelim <- par("usr")[1:2]
    mostattributes(timelim) <- attributes(from)
    axis(side = 3, at = prettyDate(timelim),
        labels = prettyDate(timelim, do.format=TRUE))
})

devAskNewPage(FALSE)


Is it appropriate / desirable for this to be incorporated into R?


-- 
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
-- 
http://www.neurofractal.org/felix/



More information about the R-devel mailing list