[R] Change plot order in lattice xyplot

William Dunlap wdunlap at tibco.com
Wed Sep 8 18:53:40 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Sam Albers
> Sent: Wednesday, September 08, 2010 9:42 AM
> To: r-help at r-project.org
> Subject: Re: [R] Change plot order in lattice xyplot
> 
> 
> Prior to creating a plot I usually just order the factor 
> levels to the order
> I want them in. So for your example I would do:
> 
> #Create some data
> library(lattice)
> x <- runif(100, 0, 20) 
> df <- data.frame(x) 
> df$y <- (1:10) 
> df$Month <- c("October", "September", "August", "July","June") 
> 
> #Plot the figure
> plt <-xyplot(x~y | Month, data =df,
> 		 layout=c(5,1),
> 		 xlab="Log density from hydroacoustics 
> (integration)",    
>                  ylab="Log density from Tucker trawl",
>                  main="Density estimates, Tucker Trawl", 
>                  cex=1.5) 
> 
> #Factor levels aren't in the order you want them in. Reorder 
> them how you
> want.
> df$Month <- factor(df$Month, levels=c("June","July","August", 
> "September",
> "October"), order=TRUE)

You don't need to use the ordered=TRUE argument there
and it may be mildly inappropriate.  The command
   factor(theData, levels=theLevels)
does not reorder theLevels.  Using ordered=TRUE means
to make an object of class "ordered" (a subclass
of "factor").  Modelling functions like lm() deal with
ordered factors a bit differently than unordered factors,
but the ordered/factor distinction has no effect on how
most graphics and data manipulation functions deal with them.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> #Plot again.
> plt
> 
> HTH,
> 
> Sam
> 
> -- 
> View this message in context: 
> http://r.789695.n4.nabble.com/Change-plot-order-in-lattice-xyp
lot-tp2531542p2531619.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list