[R] xyplot() - can you control how the plots are ordered?

Dieter Menne dieter.menne at menne-biomed.de
Sun Mar 8 10:24:31 CET 2009


AR <anandram <at> gmail.com> writes:

> I want to control the plots in the output of the xyplot(). It is
> easier to explain it through an example:
> 
.. modified example see below
> 
> The output of the xyplot() is sorted by the alphabetical order of
> months (First Apr, then Aug so on). I need to output such that the
> order of the months is maintained, i.e. plot of Jan first, then Feb,
> Mar ... so on.

The bad thing happens during the implicit conversion to factors
by xyplot. Create your factors explicitly, and make sure the 
given order is used instead of the default
sort(unique.default(x), na.last = TRUE)
You probably also would like to add as.table = TRUE to 
get Jan at the top.

Dieter


library(lattice);
# months
months <- c("Jan", "Feb", "Mar",
           "Apr", "May", "Jun",
           "Jul", "Aug", "Sep",
           "Oct", "Nov", "Dec")
months = factor(months,levels=months)

n <- length(months);
m <- 10;

mdata <- data.frame(x = runif(m*n), y = runif(m*n), z = rep(months,
each = m));
h <- xyplot(y ~ x | z, data = mdata,as.table=TRUE);
plot(h);




More information about the R-help mailing list