Peter_Kappes at fws.gov
2009-Jul-07 22:03 UTC
[R] How to re-order panels and y-axis values in trellis display using lattice
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. Peter USF&WS San Francisco Bay National Wildlife Refuge Complex Common Murre Restoration Project peter_kappes@fws.gov [[alternative HTML version deleted]]
Deepayan Sarkar
2009-Jul-08 12:20 UTC
[R] How to re-order panels and y-axis values in trellis display using lattice
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