[R] lattice: limits in reversed order with relation="same"

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Aug 30 14:16:49 CEST 2010


On Mon, Aug 30, 2010 at 3:56 AM, Thaler,Thorn,LAUSANNE,Applied
Mathematics <Thorn.Thaler at rdls.nestle.com> wrote:
>> No (in fact that wouldn't work anyway), you can simply do
>>
>> xyplot(y~x|z, xlim = rev(extendrange(x)))
>>
>> The point is that in this case you have to explicitly specify a
>> pre-computed limit, and cannot rely on the prepanel function to give
>> you a nice default.
>
> Thanks that does the trick. Because I'm curious: is extendrange(x) the default way to
> calculate the limits if not given?

Well, it's actually lattice:::extend.limits(range(x)), but
extendrange() does basically the same thing, and is available to the
user (i.e., exported), albeit from a different package.

> By the way, it would be very nice if one could write
>
>
> df <- data.frame(x=1:3, y=2:4, df.col=3:5)
> xyplot(y~x, data=df, col=df.col)
>
> instead of
>
> xyplot(y~x, data=df, col=df$df.col)

And what if, say,

df <- data.frame(x=1:3, y=2:4, df.col=c("one", "two", "three"))

and you still want the different 'df.col' values to have different colors?

The Trellis philosophy thinks of 'df.col' as a "grouping variable" (to
be distinguished by color etc. depending on context), so you would say

xyplot(y~x, data=df, groups=df.col)

ggplot2 goes further and allows you to associate different graphical
parameters (color, plotting character, etc.) with different variables,
which is a bit harder to do with lattice. In either case, the point is
that except in special cases, you cannot expect the values of a
variable to be exactly interpretable as valid R color specifications.

You can of course do

with(df, xyplot(y~x, col=df.col))

-Deepayan



More information about the R-help mailing list