2

It is easy to change the default strip height for lattice plots: the par.strip.text argument is all that one needs. But is there a simple way to to have strips of different heights within one multi-panel lattice plot?

I have in mind a plot with two rows of panels. The height of the strips in the first row would differ from the height of the strips in the second row.

I think that I can create such a figure by creating two plots –– one for the first row, another for the second row –– and then using grid.layout to position them. But I'd like to know if there is a more straightforward way to create such a figure.

user697473
  • 2,165
  • 1
  • 20
  • 47
  • I'm tempted to call this a duplicate of [this](http://stackoverflow.com/q/17825201/324364) Q, even though it's a ggplot Q. That's the only method I can think of, though, and it _should_ work. – joran Jul 26 '13 at 18:44
  • @joran -- Thanks. But I'm interested in having strips of different heights within a single plot, and as best I can tell, that Q is about a different problem. (I also am not sure of how I would adapt the ggplot code there for use with lattice. I've tried just varying the `lines` element in the `par.strip.text` argument, so that `lines` varies by panel. But that doesn't work: `lines` needs to be a single number.) – user697473 Jul 26 '13 at 19:07
  • No. You would just add different number of newline characters to each level. That's the point: adding newline characters to the actual variable levels. – joran Jul 26 '13 at 19:08
  • 1
    So I just checked, and that method (adding newline characters) doesn't seem to have any effect in lattice. Oh well. – joran Jul 26 '13 at 19:18

1 Answers1

5

I modified an example from this question (which is a much closer duplicate) and managed to achieve this:

bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
stripHt <- rep(c(-1,0),each = 3)

# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
    panel.rect(0, stripHt[which.panel], 1, 1,
               col = bgColors[which.panel],
               border = 1)
    panel.text(x = 0.5, y = 0.5,
               font=2,
               lab = factor.levels[which.panel],
               col = txtColors[which.panel])
}    
xyplot(yield ~ year | site, data = barley, strip=myStripStyle)

enter image description here

Ignore the horrible colors. You get the point, we're just using a custom strip function.

Community
  • 1
  • 1
joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Quick note: `which.panel` no longer seems to work (R 3.1.3, lattice 0.20-30). But `panel.number()` does the same. – user697473 Dec 29 '15 at 05:59