[R] boxplot group labels

Marc Schwartz MSchwartz at mn.rr.com
Sun May 14 22:01:46 CEST 2006


On Sun, 2006-05-14 at 19:29 +0000, Alexander Nervedi wrote:
> Hi All,
> 
> I would like to control the order in which my boxplots are drawn. When i 
> issue the lines below I find that group "a" get plotted lowest, while the 
> highest letter grade is plotted on the top. I want to plot the letter grades 
> in descending order and I was wondering if tere is a quick way to do that.
> 
> thanks.
> Alex.
> 
> 
> play <- data.frame(code = letters[round(runif(100)*10+1)], income = 
> runif(100))
> with(play, boxplot(income~code, horizontal = TRUE, boxwex = 0.1))


The boxplot group order is based upon the factor levels for the RHS of
the formula. By default, the levels are done with an alpha sort.

So, to change the order, you can use the reorder() function, which has a
factor method:

with(play, boxplot(income ~ reorder(code, rev(levels(code))), 
                   horizontal = TRUE, boxwex = 0.1))

or you could break out the reorder() component into a separate line of
code if you find that easier to read.

See ?reorder for more information and an example.

HTH,

Marc Schwartz




More information about the R-help mailing list