[R] problem with chron scales in lattice

Deepayan Sarkar deepayan at stat.wisc.edu
Wed Jun 1 21:31:00 CEST 2005


On Wednesday 01 June 2005 12:12, Waichler, Scott R wrote:
> I can't get the scales parameter in xyplot of lattice to work as I
> expected.  I'm using R-2.1.0 and lattice 0.11-8.  There should be year
> labels from 1992 to 2004 for the x axis in the plot below, but instead
> only a few of them appear, and in the wrong spots, as if the coordinate
> system has changed after finishing with the panel function.

This is rather odd. The coordinate system seems fine, since the following 
works as it should:


trellis.focus("panel", 1, 2)
panel.axis(side = "top", at = as.numeric(x.at), 
          outside = F, half = F, labels = x.labels, 
          rot = 0)

I'll try to figure out what the problem is. A temporary workaround is to 
explicitly coerce x.limits and x.at to numeric:

    ...
    xlim = as.numeric(x.limits),
    scales = list(x = list(alternating = T, 
                  at = as.numeric(x.at), 
                  labels = x.labels, tck=0)),
    ...


> library(chron)
> library(lattice)
>
> # vertical grid lines at start of each year
> startdate <- "1/1/1992"
> enddate   <- "1/1/2005"
> startdatetime <- chron(dates=startdate, times="00:00:00")
> enddatetime <- chron(dates=enddate, times="00:00:00")
> x.lines <- as.chron(seq.dates(startdate, enddate, by="years"))
> x.limits <- c(as.chron(startdate), as.chron(enddate))
>
> # year labels on x axis (centered between grid lines)
> x.at <- as.chron(seq.dates("7/1/1992", "7/1/2004", by="years"))
> x.labels <- format(as.Date(dates(x.at)), "%Y")
>
> # dummy data
> x <- x.at[1:2]
> y <- c(105, 105)
> id <- c(1,2)
>
> print(xyplot(y ~ x | id,
>    panel = function(x, y, subscripts, ...) {
>      panel.abline(v = x.lines, col = "lightgray", lty = 1, lwd= 1.0)
>      # function to plot data would go here
>    },  # end of panel
>    as.table=T, layout=c(1,2), main="",
>    xlim=x.limits,
>    scales = list(x = list(alternating = T, at = x.at, labels = x.labels,
> tck=0)),
>    strip = F
>  ) # end xyplot()
> ) # end print()
>
> Thanks for any help,
>
> Scott Waichler
> Pacific Northwest National Laboratory
> scott.waichler at pnl.gov




More information about the R-help mailing list