[R] How to re-order panels and y-axis values in trellis display using lattice

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 8 14:20:49 CEST 2009


On Tue, Jul 7, 2009 at 3:03 PM, <Peter_Kappes at fws.gov> wrote:
> Hi,
>
> I have been trying to re-order several items in a trellised barchart
> display in lattice, but can't seem to figure it out.
>
> ###sample code, Stage and Colony have 2 and 3 levels respectively.
>
> barchart(Activity ~ Percent | Stage + Colony, data = Percent.df,
>  horizontal = TRUE, layout = c(2,3),
>  xlab = "Percent Time Engaged in Activity",
>  ylab = "Activity")
>
> This essentially produces the display I would like, only I would like to
> have the second column first and the first and third panels switched.  In
> addition, I would like to re-order the y-axis labels ("Activities") so
> that they are in alphabetical order starting with A at the top...currently
> A is at the bottom.  I have been able to switch the actual data in the
> panels useing barchart(rev(Activity~ Percent...but cannot get the actual
> labels to switch.  Thanks in advance.

You need to read up on levels() of factors; in both cases, the
ordering is determined by the levels, which are by default ordered
lexically for character vectors, but can be specified manually, e.g.

factor(Activity, levels = rev(sort(unique(Activity)))

For the first question, there is a shortcut to reorder levels of
conditioning variables:

p <- barchart(Activity ~ Percent | Stage + Colony, data = Percent.df, ...)
p[, c(2, 3, 1)]

-Deepayan




More information about the R-help mailing list