[R] current.panel.limits() of lattice returning NaN limits - why?
Deepayan Sarkar
deepayan.sarkar at gmail.com
Fri Aug 12 09:22:41 CEST 2011
On Fri, Aug 12, 2011 at 12:15 PM, Fredrik Karlsson <dargosch at gmail.com> wrote:
> Hi,
>
> I need a custom axis function for a plot, but it seems
> that current.panel.limits() sometimes returns NaN limits for the plot, which
> it much harder to calculate anything sensible.
> An illustration:
>
> Given this axis function:
>
>
> vs.axis <- function(...){
> xlim <- current.panel.limits()$xlim
> ylim <- current.panel.limits()$ylim
>
> # Debug code
> print(list(ylim=ylim,xlim=xlim))
>
> xat <- pretty(seq(xlim[1],xlim[2],100),n=5)
> yat <- pretty(seq(ylim[1],ylim[2],100),n=4)
> xlab <- sub("-","",as.character(xat))
> ylab <- sub("-","",as.character(yat))
> panel.axis(side="top",at=xat,labels=xlab)
> panel.axis(side="right",at=yat,labels=ylab)
> }
>
> and the attached data set, I get this output:
(The attachment didn't come through.)
>> xyplot(F1 ~F2,data=pb,axis=vs.axis)
> $ylim
> [1] NaN NaN
>
> $xlim
> [1] 346.5 3823.5
>
> Error in if (del == 0 && to == 0) return(to) :
> missing value where TRUE/FALSE needed
>
> What's wrong? Is there a more robust way of getting the x- and y- limits?
You are doing the equivalent of
library(grid)
pushViewport(viewport(0.5, 0.5, width = 0.8, height = 0, xscale = c(0, 10)))
current.panel.limits()
(Note the height=0).
The axis function is called four times, once for each side. On the top
and left sides, it is actually called with the strip viewports active,
because the axis annotation goes outside the strip, not the panel. In
your case there are no strips, which basically mean a 0-height strip.
Your code will work (although will not give what you want) if you try
something like
xyplot(rnorm(10) ~ 1:10 | gl(1, 10), axis=vs.axis,
strip = TRUE, strip.left = TRUE)
In real applications of custom axis functions, one usually writes code
conditioned on the value of the 'side' argument.
-Deepayan
More information about the R-help
mailing list