How to change the order of the panels in simple Lattice graphs

16,569

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
Share:
16,569
BDM
Author by

BDM

Updated on June 09, 2022

Comments

  • BDM
    BDM almost 2 years

    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
    BDM almost 13 years
    @Roman Thanks!! such and easy solution, the one thing I did not try.
  • Aaron left Stack Overflow
    Aaron left Stack Overflow almost 13 years
    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.
  • BDM
    BDM almost 13 years
    @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.
  • Roman Luštrik
    Roman Luštrik almost 13 years
    @BDM, it gets easier with time. :)
  • Marek
    Marek almost 13 years
    @BDM Roman solution is better but this can be done with index.cond. Try index.cond=list(c(4,1,2,3)).
  • Roman Luštrik
    Roman Luštrik almost 13 years
    Thanks @Marek, I knew there was one clean way of doing it but was tight on time to look it up.
  • Andri Signorell
    Andri Signorell about 6 years
    levels(rate$Temp) <- c("12", "9", "18", "15") will rename the levels not reorder them - right?