Search code examples
rlatticelevelplot

How to get separate z scale for multiple levelplots (lattice)


Here is my attempted code.

attach(mtcars) 
levelplot(mpg ~ wt * hp|gear, data = mtcars,labels=FALSE ,scales=list(relation="free"))

The result is this plot. enter image description here

I wish to know how to (1) add local z colorbar for each levelplots and (2) how to make each levelplot's title shows as gear 4, gear 3 and gear 5 instead of just "gear". The desired result is something like this following figure (the colorbar is here is cut-and-paste so it isn't local range like it should be). I have checked help and look up online but couldn't find a solution yet. Desired plot


Solution

  • Because mtcars$gear is of class "numeric", you plot is using the "shingled" strip style associated with numeric conditioning variables. It sounds like you'd rather gear number be treated as a categorical variable, so you should convert it to a "factor" before conditioning on it.

    Here's what I would do:

    gearFac <- factor(mtcars$gear, levels=3:5, labels=paste0("gear", 3:5))
    levelplot(mpg ~ wt * hp|gearFac, data = mtcars,
              labels = FALSE, scales = list(relation="free"))
    

    enter image description here