Matthew Snyder
Matthew Snyder

Reputation: 443

How do I change the axis on a single panel in a lattice plot

I have been trying for awhile now to solve a problem with lattice plots. I have an uneven number of panels and so if I impose non-alternating axis labels one of the panels doesn't have an X axis label.

The following code is not for my specific project but uses an R dataset that is available to all:

densityplot(~Taste|Wine,data=WineTasting, as.table=TRUE, 
        scales=list(alternating=FALSE), 
        panel=function(x,y,...){ 
           panel.densityplot(x,plot.points=FALSE) 
           } 
) 

And this produces a plot that looks like the first image: Image 1

See how "Wine B" has no X axis. I want all the panels to have axis tick marks and numeric labels on the bottom. So if I insert panel.axis(side="bottom") I get an error message and a plot that looks like like the second image: Image 2

I also tried the following code:

densityplot(~Taste|Wine,data=WineTasting, as.table=TRUE, 
        scales=list(alternating=FALSE), 
        panel=function(x,y,...){ 
          panel.densityplot(x,plot.points=FALSE) 
            }, 
        if(isTRUE(panel.number(2))){ 
          panel.axis(side="bottom") 
        } 
) 

But this just produces an error message that reads:

"Error in get("lattice.status", envir = .LatticeEnv)[[prefix]][[name]] : subscript out of bounds"

Can anyone help me get an axis on the bottom of that single panel (Wine B)?

Thanks, Matt

Upvotes: 2

Views: 976

Answers (1)

Johan Larsson
Johan Larsson

Reputation: 3694

You have to get a little creative with clipping.

library(lattice)

densityplot(~ mpg | cyl, data = mtcars, as.table = TRUE, 
    scales = list(alternating = FALSE),
    par.settings = list(clip = list(panel = "off")),
    panel = function(x, y, ...) { 
       panel.densityplot(x, plot.points = FALSE, ...)
       if (panel.number() == 2) {
         panel.axis(side = "bottom", outside = TRUE)
       }
    }
) 

Imgur

Upvotes: 2

Related Questions