How to change the order of the panels in simple Lattice graphs
16,569
I converted Temp
to factor and got this:
You can temper with factor order like so:
levels(rate$Temp) <- c("12", "9", "18", "15") #custom factor order
Author by
BDM
Updated on June 09, 2022Comments
-
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:
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
Thanks for the help
-
BDM almost 13 years@Roman Thanks!! such and easy solution, the one thing I did not try.
-
Aaron left Stack Overflow almost 13 yearsTo 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 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 almost 13 years@BDM, it gets easier with time. :)
-
Marek almost 13 years@BDM Roman solution is better but this can be done with
index.cond
. Tryindex.cond=list(c(4,1,2,3))
. -
Roman Luštrik almost 13 yearsThanks @Marek, I knew there was one clean way of doing it but was tight on time to look it up.
-
Andri Signorell about 6 yearslevels(rate$Temp) <- c("12", "9", "18", "15") will rename the levels not reorder them - right?