14

Hi I am using the following code to generate an xyplot using lattice

xyplot(Rate~Weight|Temp, groups=Week, rate,
 pch=c(15,16,17,3), col=c("blue","red","green","purple"),
 as.table=TRUE,
 xlab="Weight (gr)", ylab="Rate (umol/L*gr)",
 main="All individuals and Treatments at all times",
 strip=strip.custom(strip.names=1),
 key=
 list(text=list(c("Week","1","2","6","8")),
 points=list(pch=c(NA,15,16,17,3),col=c(NA,"blue","red","green","purple")),
 space="right")
 )

This gives me the following plot: enter image description here

Now after changing the code to include panel order as suggested:

xyplot(Rate~Weight|Temp, groups=Week, rate,
 index.cond=list(c(4,1,2,3)),#this provides the order of the panels
 pch=c(15,16,17,3), col=c("blue","red","green","purple"),
 as.table=TRUE,
 xlab="Weight (gr)", ylab="Rate (umol/L*gr)",
 main="All individuals and Treatments at all times",
 strip=strip.custom(strip.names=1),
 key=
 list(text=list(c("Week","1","2","6","8")),
 points=list(pch=c(NA,15,16,17,3),col=c(NA,"blue","red","green","purple")),
 space="right")
 )

and we get the correct order enter image description here

Thanks for the help

BDM
  • 464
  • 2
  • 7
  • 15
  • +1 for providing data and tried solution. Would be nicer if the data was in R-eady format (using `dput` or constructing a `data.frame`/`matrix` using `sample`, `runif`, `letters`...). – Roman Luštrik Jun 15 '11 at 16:23
  • @Roman I included the working code and the graphs to ilustrate hope it helps :) – BDM Jun 16 '11 at 10:10

1 Answers1

8

I converted Temp to factor and got this:

enter image description here

You can temper with factor order like so:

levels(rate$Temp) <- c("12", "9", "18", "15") #custom factor order
Roman Luštrik
  • 64,404
  • 24
  • 143
  • 187
  • @Roman Thanks!! such and easy solution, the one thing I did not try. – BDM Jun 15 '11 at 14:31
  • 5
    To clarify, lattice orders the panel based on the order of the factor, and when factors are created, by default, they're sorted first. If you make a factor directly from the numeric variable, it will do what you want, but if you convert to a character first, it will sort alpha-numerically, which is what happened to you. – Aaron left Stack Overflow Jun 15 '11 at 14:35
  • @Aron Thanks, needless to say I am not a programmer and I am new to both R and programming so I keep getting stuck with easy things. I was trying to used the index.cond with in the plot code but was getting nowhere. – BDM Jun 15 '11 at 14:39
  • @BDM, it gets easier with time. :) – Roman Luštrik Jun 15 '11 at 16:21
  • 7
    @BDM Roman solution is better but this can be done with `index.cond`. Try `index.cond=list(c(4,1,2,3))`. – Marek Jun 15 '11 at 19:11
  • Thanks @Marek, I knew there was one clean way of doing it but was tight on time to look it up. – Roman Luštrik Jun 16 '11 at 08:47
  • levels(rate$Temp) – Andri Signorell Feb 21 '18 at 06:53