Hi, I want to control the plots in the output of the xyplot(). It is easier to explain it through an example: #------------------------------------------------------------- library(lattice); # months months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 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); plot(h); #------------------------------------------------------------- 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. I tried searching on the web and I couldn't find any or probably didn't understand. I would be much obliged if someone could help. Thanks Anand.
Mark Difford
2009-Mar-08 09:14 UTC
[R] xyplot() - can you control how the plots are ordered?
Hi Anand,>> 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.Because the levels of your factor are sorted this way: mdata$z levels(mdata$z) So reorder them, e.g. ?reorder, or use the following if you don't want to permanently reorder levels: ## ignore the warning message h <- xyplot(y ~ x | relevel(z,ref=c("Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec")), data = mdata) plot(h) Another way of doing it is to use the index.cond argument to xyplot (see: ?xyplot). Regards, Mark. anandram wrote:> > Hi, > > I want to control the plots in the output of the xyplot(). It is > easier to explain it through an example: > > #------------------------------------------------------------- > library(lattice); > # months > months <- c("Jan", "Feb", "Mar", > "Apr", "May", "Jun", > "Jul", "Aug", "Sep", > "Oct", "Nov", "Dec"); > > 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); > plot(h); > #------------------------------------------------------------- > > 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. > > I tried searching on the web and I couldn't find any or probably > didn't understand. I would be much obliged if someone could help. > > Thanks > Anand. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/xyplot%28%29---can-you-control-how-the-plots-are-ordered--tp22395744p22396032.html Sent from the R help mailing list archive at Nabble.com.
Dieter Menne
2009-Mar-08 09:24 UTC
[R] xyplot() - can you control how the plots are ordered?
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);
Thanks Mark and Dieter. That helps. Anand On Mar 8, 4:24?am, Dieter Menne <dieter.me... at menne-biomed.de> wrote:> AR<anandram <at> gmail.com> writes: > > > I want to control the plots in the output of thexyplot(). It is > > easier to explain it through an example: > > .. modified example see below > > > > > The output of thexyplot() 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 > byxyplot. 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); > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.