[R] lattice: option to sort x when type = l

Prasenjit Kapat kapatp at gmail.com
Tue Jul 27 07:04:24 CEST 2010


On Tue, Jul 27, 2010 at 12:41 AM, Deepayan Sarkar
<deepayan.sarkar at gmail.com> wrote:
> On Mon, Jul 26, 2010 at 9:11 PM, Prasenjit Kapat <kapatp at gmail.com> wrote:
>> Hi,
>>
>> (please Cc me)
>>
>> In xyplot (), type = "l" (or one that includes "l", *el*) is
>> (generally) meaningful only when the 'x' variable is sorted. In
>> practice, one either sorts the data frame before hand or writes a tiny
>> panel function which sorts the supplied x and then calls the default
>> panel.xyplot(). Trouble arises when there is a conditional variable as
>> well as a groups argument.
>>
>> Before giving a reproducible example, is there anything even simpler
>> that I have missed?

[snip]

>
> Here are two simple enough solutions:
>
> ## uses panel.average
>
> xyplot(y ~ x/1000 | C, groups = G, data = D, type = c("p", "a"))
>
> ## more efficient, as no tapply involved
>
> panel.xyplot.sorted <- function(x, y, ...)
> {
>    o <- order(x)
>    panel.xyplot(x[o], y[o], ...)
> }
>
> xyplot(y ~ x/1000 | C, groups = G, data = D, type = "o",
>       panel = panel.superpose,
>       panel.groups = panel.xyplot.sorted)
>

Uh, indeed missed that one! Thanks.

-- 
Prasenjit



More information about the R-help mailing list