[R] identical scales per panel with relation="free"

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Apr 16 16:55:27 CEST 2008


On 4/16/08, Andreas Krause <andreas at elmo.ch> wrote:
>
>  Using xyplot, I am plotting observed versus predicted values conditional to the values of a third variable.
>  I would thus like to have an aspect ratio of 1 and the same axis range.
>  Since the range of the values differs substantially between panels, the axis ranges shall be different for each panel (along the lines of relation="free"). However, x- and y-limits shall still be the same for a given panel.
>  Any ideas on how to achieve that?
>
>  # Example (that produces different x/y-limits per panel)
>  set.seed(3484)
>  x <- data.frame(
>     "x"=c(rnorm(10), rnorm(10, 10))
>  )
>  x$y <- x$x + rnorm(length(x$x))
>  x$z <- c(rep(c("A", "B"), c(10, 10)))
>
>  print(xyplot(y ~ x | z, data=x,
>     aspect=1,
>     scales=list(
>         relation="free"
>     ),
>     panel=function(x, y, ...)
>     {
>         panel.abline(0, 1)
>         panel.xyplot(x, y, ...)
>     }
>  ))

Try this:

print(xyplot(y ~ x | z, data=x,
   aspect=1,
   scales=list(
       relation="free"
   ),
   prepanel = function(x, y, ...) {
       r <- range(x, y, ...)
       list(xlim = r, ylim = r)
   },
   panel=function(x, y, ...)
   {
       panel.abline(0, 1)
       panel.xyplot(x, y, ...)
   }
))

-Deepayan



More information about the R-help mailing list