0

How do I sort a boxplot in ggplot?

Here's what I'm trying to plot:

qplot(  row.names(pcaDF),pcaDF[,1],data=pcaDF,geom="boxplot") + coord_flip() 

and here's the structure of pcaDF

> str(pcaDF)
'data.frame':   108 obs. of  1 variable:
 $ sort(plotdata[, 1], decreasing = F): num  -5.89 -5.52 -4.66 -4.54 -3.92 ...
Marek
  • 49,472
  • 15
  • 99
  • 121
Jim
  • 1
  • 1
  • 4
    It's hard to answer without a reproducible example, but in brief, create a data frame and then use reorder to change the order of the rownames. – hadley Oct 30 '10 at 23:40

2 Answers2

2

Sorting in ggplot is regulated by the levels in the factors. To control the sorting of factor: A in df:

df$A = factor(df$A , level = sort(unique(df$A)) )
user3030872
  • 367
  • 1
  • 6
  • 14
1

This question is answered here: ggplot: how to specify vertical order of multiple boxplots?

Others finding this question and wanting to reorder (sort) other data with ggplot may find the following post useful. How to order breaks with ggplot / geom_bar

If other searchers still are lost trying using the keyword "order" in place of "sort" as this seems more fruitful from experience. :-)

Community
  • 1
  • 1
computermacgyver
  • 802
  • 7
  • 15