Message-ID: <eb555e661001280334p5e870a7eg1d5b2b93629942b4@mail.gmail.com>
Date: 2010-01-28T11:34:11Z
From: Deepayan Sarkar
Subject: different x-axes in Lattice
In-Reply-To: <3f4440821001240524i7f1f25a4ob45d6d95736f8d43@mail.gmail.com>
On Sun, Jan 24, 2010 at 5:24 AM, Robert Ruser <robert.ruser at gmail.com> wrote:
> I use lattice package and 'barchart' to build a chart. I have a
> problem with setting different x-axes. Some x categories are missing
> but they are display and I don't want. I use scales = list(y =
> "free",x="free") but it works only for y-axes. Simple example:
>
> package(lattice)
> barchart(yield ~ variety | site, data = barley,
> ? ? ? ?groups = year, layout = c(1,6),
> ? ? ? ?scales = list(y = "free",x="free"),
> ? ? ? ?auto.key=list(rectangles = TRUE, space = "bottom"),
> ? ? ? ?)
>
> Let's assume that in the first panel ?'Waseca' ?the category 'Velvet'
> are not available in data but in the chart there is a empty place. If
> more categories are not available it looks bad. Simple modification to
> illustrate my problem:
>
> barley2 <- barley[barley[,2]!="Velvet" | barley[,4]!="Waseca",]
> barchart(yield ~ variety | site, data = barley2,
> ? ? ? ?groups = year, layout = c(1,6),
> ? ? ? ?scales = list(y = "free",x="free"),
> ? ? ? ?auto.key=list(rectangles = TRUE, space = "bottom"),
> ? ? ? ?)
The short answer is that empty factor levels will be omitted only if
they are at the boundary (because what are actually plotted on the
x-axis here are the numeric factor codes). For example,
barchart(yield ~ reorder(variety, yield, length) | site, data = barley2,
groups = year, layout = c(1,6),
scales = list(y = "free",x="free"))
If you want to omit levels in the middle, you need to actually change
the internal codes using 'x[drop=TRUE]' where 'x' is your factor with
the missing levels. Lattice has no built-in facility to do that, but
you can do it yourself in custom prepanel and panel functions.
-Deepayan