[R] reversing the x-axis terms on a boxplot

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Jul 25 21:05:21 CEST 2007



Dylan Beaudette said the following on 7/25/2007 11:18 AM:
> Hi,
> 
> I am able to reverse the order of plotting on regular plots (i.e. with the 
> plot() function) by manually setting the xlim variable. 
> 
> Is there some trick like this which will work for a boxplot?
> 
> * for example:
> 
> l <- sample(letters, 500, replace=TRUE)
> n <- runif(500)
> boxplot(n ~ l)
> 
> 
> this will produce a plot with the x-axis ranging from a->z ... i know that 
> these are labels, associated with an integer index-- but is there someway to 
> reverse the plotting order?
> 
> Thanks in advance,
> 
> Dylan
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Try this:

set.seed(1)
l <- sample(letters, 500, replace=TRUE)
n <- runif(500)
l <- factor(l, levels = rev(letters))
boxplot(n ~ l)

This is explained in the details section of ?boxplot.

      If multiple groups are supplied either as multiple arguments or
      via a formula, parallel boxplots will be plotted, in the order of
      the arguments or the order of the levels of the factor (see
      'factor').

This means you can create any order you want by setting the factor 
levels explicitly.

HTH,

--sundar



More information about the R-help mailing list