[R] Lattice: hiding only some strips

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Sat Jun 23 04:23:31 CEST 2007


On 6/22/07, Michael Hoffman <b3i4old02 at sneakemail.com> wrote:
> Deepayan Sarkar wrote:
> > On 6/22/07, Michael Hoffman <b3i4old02 at sneakemail.com> wrote:
> >> I am using R 2.4.0 and lattice to produce some xyplots conditioned on a
> >> factor and a shingle. The shingle merely chops up the data along the
> >> x-axis, so it is easy to identify which part of the shingle a panel is
> >> in by looking at the x-axis markings. I only want to have a strip at the
> >> top for the factor.
> >>
> >> Is this possible? I looked into calculateGridLayout() and it seems to me
> >> that there isn't an easy way to do it without rewriting that function
> >> (and others).
> >
> > It's nowhere near that complicated, you just need to write your own
> > strip function. E.g.,
> >
> > mtcars$HP <- equal.count(mtcars$hp)
> >
> > xyplot(mpg ~ disp | HP + factor(cyl), mtcars,
> >        par.strip.text = list(lines = 0.5),
> >        strip = function(which.given, which.panel, ...) {
> >            if (which.given == 2)
> >                strip.default(which.given = 1,
> >                              which.panel = which.panel[which.given],
> >                              ...)
> >        })
>
> Thank you for this response. But it looks like I poorly specified the
> problem. I only want to have a strip at the very top of the plot, not at
> the top of each panel.
>
> You can probably understand why I want this better if we take your
> example and swap the givens around:
>
> xyplot(mpg ~ disp | factor(cyl) * HP, mtcars,
>         par.strip.text = list(lines = 0.5),
>         strip = function(which.given, which.panel, ...) {
>             if (which.given == 1)
>                 strip.default(which.given = 1,
>                               which.panel = which.panel[which.given],
>                               ...)
>         })
>
> So now I have 4, 6, and 8 at the top of every row of panels as a label
> for cyl. But I don't need that--really I only need 4, 6, and 8 at the
> very top (or bottom) of the plot, just like with default settings I only
> get the axis labels at the top and bottom of the plot, not duplicated
> for every panel.
>
> What I want is to draw strips at the very top of the plot and not to
> draw strips that are between panels.
>
> Can this be done?

Sure.

xyplot(mpg ~ disp | factor(cyl) * HP, mtcars,
       par.strip.text = list(lines = 0.5),
       strip = function(which.given, which.panel, ...) {
           if (which.given == 1)
               strip.default(which.given = 1,
                             which.panel = which.panel[which.given],
                             ...)
       },
       par.settings =
       list(layout.heights =
            list(strip = rep(c(0, 1), c(5, 1)))))

-Deepayan



More information about the R-help mailing list