Ordering boxplot factors

4 messages
Open this post in threaded view
|

Ordering boxplot factors

 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-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

Re: Ordering boxplot factors

 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-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html