Ordering boxplot factors

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ordering boxplot factors

Joseph LeBouton
Hi all,

what a great help list!  I hope someone can help me with this puzzle...

I'm trying to find a simple way to do:

boxplot(obs~factor)

so that the factors are ordered left-to-right along the x-axis by
median, not alphabetically by factor name.

Complicated ways abound, but I'm hoping for a magical one-liner that'll
do the trick.

Any suggestions would be treasured.

Thanks,

-jlb
--
************************************
Joseph P. LeBouton
Forest Ecology PhD Candidate
Department of Forestry
Michigan State University
East Lansing, Michigan 48824

Office phone: 517-355-7744
email: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Ordering boxplot factors

Marc Schwartz (via MN)
On Thu, 2006-01-05 at 20:27 -0600, Joseph LeBouton wrote:

> Hi all,
>
> what a great help list!  I hope someone can help me with this puzzle...
>
> I'm trying to find a simple way to do:
>
> boxplot(obs~factor)
>
> so that the factors are ordered left-to-right along the x-axis by
> median, not alphabetically by factor name.
>
> Complicated ways abound, but I'm hoping for a magical one-liner that'll
> do the trick.
>
> Any suggestions would be treasured.
>
> Thanks,
>
> -jlb


Using the first example in ?boxplot, which is:

boxplot(count ~ spray, data = InsectSprays, col = "lightgray")



Get the medians for 'count by spray' using tapply() and then sort the
results in increasing order, by median:

  med <- sort(with(InsectSprays, tapply(count, spray, median)))

> med
   C    E    D    A    F    B
 1.5  3.0  5.0 14.0 15.0 16.5


Now do the boxplot, setting the factor levels in order by median:

  boxplot(count ~ factor(spray, levels = names(med)),
          data = InsectSprays, col = "lightgray")


So...technically two lines of code.

HTH,

Marc Schwartz

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Ordering boxplot factors

Brian Ripley
On Thu, 5 Jan 2006, Marc Schwartz wrote:

> On Thu, 2006-01-05 at 20:27 -0600, Joseph LeBouton wrote:
>> Hi all,
>>
>> what a great help list!  I hope someone can help me with this puzzle...
>>
>> I'm trying to find a simple way to do:
>>
>> boxplot(obs~factor)
>>
>> so that the factors are ordered left-to-right along the x-axis by
>> median, not alphabetically by factor name.

The thing to realize is that they are not alphabetic, but ordered by
factor levels.  So the key is to set the levels.  (The help page for
boxplot does say that, as I was relieved to find.)

>> Complicated ways abound, but I'm hoping for a magical one-liner that'll
>> do the trick.
>>
>> Any suggestions would be treasured.
>>
>> Thanks,
>>
>> -jlb
>
>
> Using the first example in ?boxplot, which is:
>
> boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
>
>
>
> Get the medians for 'count by spray' using tapply() and then sort the
> results in increasing order, by median:
>
>  med <- sort(with(InsectSprays, tapply(count, spray, median)))
>
>> med
>   C    E    D    A    F    B
> 1.5  3.0  5.0 14.0 15.0 16.5
>
>
> Now do the boxplot, setting the factor levels in order by median:
>
>  boxplot(count ~ factor(spray, levels = names(med)),
>          data = InsectSprays, col = "lightgray")
>
>
> So...technically two lines of code.

This was answered yesterday in terms of bwplot.  See ?reorder.factor
for the same example done using reorder.factor.  That will give you the
single line asked for, and be self-explanatory.

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Ordering boxplot factors; thank you!

Joseph LeBouton
Profs. Ripley and Schwartz,

Thank you both very much for the suggestions!  These are exactly what I
was looking for.  I'll re-read the boxplot help yet again; every time I
read it something essential worms its way into my consciousness, but it
enters more freely when I have a hint where to look.  The sad truth is
that I was heading for a ~10-line hacker solution.  You've saved me from
my usual code-writing ignomy.

Happy new year,

-Joseph

Prof Brian Ripley wrote:

> On Thu, 5 Jan 2006, Marc Schwartz wrote:
>
>> On Thu, 2006-01-05 at 20:27 -0600, Joseph LeBouton wrote:
>>
>>> Hi all,
>>>
>>> what a great help list!  I hope someone can help me with this puzzle...
>>>
>>> I'm trying to find a simple way to do:
>>>
>>> boxplot(obs~factor)
>>>
>>> so that the factors are ordered left-to-right along the x-axis by
>>> median, not alphabetically by factor name.
>
>
> The thing to realize is that they are not alphabetic, but ordered by
> factor levels.  So the key is to set the levels.  (The help page for
> boxplot does say that, as I was relieved to find.)
>
>>> Complicated ways abound, but I'm hoping for a magical one-liner that'll
>>> do the trick.
>>>
>>> Any suggestions would be treasured.
>>>
>>> Thanks,
>>>
>>> -jlb
>>
>>
>>
>> Using the first example in ?boxplot, which is:
>>
>> boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
>>
>>
>>
>> Get the medians for 'count by spray' using tapply() and then sort the
>> results in increasing order, by median:
>>
>>  med <- sort(with(InsectSprays, tapply(count, spray, median)))
>>
>>> med
>>
>>   C    E    D    A    F    B
>> 1.5  3.0  5.0 14.0 15.0 16.5
>>
>>
>> Now do the boxplot, setting the factor levels in order by median:
>>
>>  boxplot(count ~ factor(spray, levels = names(med)),
>>          data = InsectSprays, col = "lightgray")
>>
>>
>> So...technically two lines of code.
>
>
> This was answered yesterday in terms of bwplot.  See ?reorder.factor
> for the same example done using reorder.factor.  That will give you the
> single line asked for, and be self-explanatory.
>

--
************************************
Joseph P. LeBouton
Forest Ecology PhD Candidate
Department of Forestry
Michigan State University
East Lansing, Michigan 48824

Office phone: 517-355-7744
email: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html