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

Prasenjit Kapat kapatp at gmail.com
Tue Jul 27 06:11:55 CEST 2010


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.

Of course, sorting the data frame before hand is the simplest
solution. But, when one wishes to plot against multiple 'x' variables
the data has to be sorted each time, which, to me, doesn't seem the
"right" way (of course, a personal opinion). Hence a feature request
to add a sort option when type = "l".

Before giving a reproducible example, is there anything even simpler
that I have missed?

Example:

Everything included:

> source ('http://www.stat.osu.edu/~pkapat/miscl/Code4xyplot_sort_type_l.R')

OR, step by step:

> library (lattice)
>
> # toy data
> D <- read.csv ('http://www.stat.osu.edu/~pkapat/miscl/Data4xyplot_sort_type_l.csv')
>
> # default behavior: obviously wrong
> xyp1 <- xyplot (y ~ x/1000 | C, groups = G, data = D, type = "o", main = 'Wrong behavior, obviously')
>
> # simplest soln:
> xyp2 <- xyplot (y ~ x/1000 | C, groups = G, data = with (D, D[do.call (order, list (x, C, G)),]), type = "o", main = 'Using pre-sorted data frame')
>
> # hack:
> # adds an additional `if' inside the `if ("l" %in% type) {...}' block of the default panel.xyplot ()
> # which reads default.args$sort.type.l (this should be set to FALSE, by default)
> source ('http://www.stat.osu.edu/~pkapat/miscl/panel.xyplot.sort.R')
>
> lattice.options (default.args = list (sort.type.l = TRUE))
> xyp3 <- update (xyp1, main  = 'Using hacked panel.xyplot', panel = function (...) panel.xyplot (...)) # using the hacked panel.xyplot ()
>
> # display:
> plot (xyp1); dev.new (); plot (xyp2); dev.new (); plot (xyp3)

Regards,
-- 
Prasenjit



More information about the R-help mailing list