[R] lattice xyplot - aligning date labels so that they align with the grid lines in panel.grid

john.gavin at ubs.com john.gavin at ubs.com
Tue Jun 20 10:57:26 CEST 2006


Hi Deepayan,

> You will need to do it manually, e.g.:
> 
> xyplot(value ~ date, data = x,
>        panel = function(x, y, subscripts, ...) {
>            panel.grid(h = -1, v = 0, col = "grey", lwd = 1, lty = 1)
>            panel.abline(v = as.Date(c("2005/01/01", "2006/01/01")),
>                         col = "grey", lwd = 1, lty = 1)
>            panel.xyplot(x, y, ...)
>        })
> 
> For more, you can put in more locations in panel.abline, and the same
> locations as tick mark positions in scales$y$at.

Thanks for the suggestion.
That seems to be what I was looking for.

This solution now allows me to specify the number of labels.
The only cost is that this has be done manually outside 
the xyplot command but I don't mind this.

x <- data.frame(
  date = seq(as.Date("2005/01/01"), as.Date("2006/06/01"), 
  length.out = 20), value = runif(20))

numLbl <- 6 # choose number of labels
xx <- seq(from = min(x$date), to = max(x$date), length.out = numLbl)

xyplot(value ~ date, data = x,
  panel = function(x, y, subscripts, ...)
  { panel.grid(h = -1, v = 0, col = "grey", lwd = 1, lty = 1)
    panel.abline(v = xx, col = "grey", lwd = 1, lty = 1)
    panel.xyplot(x, y, ...)
  },
  scale = list(x = list(at = as.numeric(xx), 
    labels = format(xx, "%b%y"), cex = 0.75, rot = 45)) 
) 

Regards,

John.
 

> -----Original Message-----
> From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com] 
> Sent: 19 June 2006 19:58
> To: Gavin, John
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: lattice xyplot - aligning date labels so that 
> they align with the grid lines in panel.grid
> 
> On 6/19/06, john.gavin at ubs.com <john.gavin at ubs.com> wrote:
> > Hi,
> >
> > I have a basic question about aligning date labels for the x-axis
> > in an xyplot so that they align with the grid lines
> > from the panel.grid argument.
> >
> > For example, with
> >
> > x <- data.frame(
> >   date = seq(as.Date("2005/01/01"), as.Date("2006/06/01"),
> >   length.out = 20), value = runif(20))
> > xyplot(value ~ date, data = x,
> >   panel = function(x, y, subscripts, ...)
> >   { panel.grid(h = -1, v = -1, col = "grey", lwd = 1, lty = 1)
> >     panel.xyplot(x, y, ...)
> >   })
> >
> > How can I get the labels on the x-axis to align
> > with the vertical grid lines?
> > i.e. I have 6 vertical grid lines by default (v = -1)
> > in this example so I would like 6 labels along the x-axis
> > at the same points, whereas I see only 2 (2005 and 2006).
> 
> You will need to do it manually, e.g.:
> 
> xyplot(value ~ date, data = x,
>        panel = function(x, y, subscripts, ...) {
>            panel.grid(h = -1, v = 0, col = "grey", lwd = 1, lty = 1)
>            panel.abline(v = as.Date(c("2005/01/01", "2006/01/01")),
>                         col = "grey", lwd = 1, lty = 1)
>            panel.xyplot(x, y, ...)
>        })
> 
> For more, you can put in more locations in panel.abline, and the same
> locations as tick mark positions in scales$y$at.
> 
> > As an alternative I would be happy to just specify the 
> number of labels
> > as long as panel.grid's vertical lines aligned with the labels.
> 
> Not possible currently, unlikely to be possible ever.
> 
> > > R.version.string
> > [1] "Version 2.3.1 (2006-06-01)"
> >
> > on Windows NT4.
> >
> > Regards,
> >
> > John.
> >
> > John Gavin <john.gavin at ubs.com>,
> > Commodities, FIRC,
> > UBS Investment Bank, 2nd floor,
> > 100 Liverpool St., London EC2M 2RH, UK.
> > Phone +44 (0) 207 567 4289
> > This communication is issued by UBS AG or an affiliate 
> ("UBS...{{dropped}}
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> 
> 
> -- 
> http://www.stat.wisc.edu/~deepayan/
> 
This communication is issued by UBS AG or an affiliate ("UBS...{{dropped}}



More information about the R-help mailing list