问题
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 ...
回答1:
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)) )
回答2:
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. :-)
来源:https://stackoverflow.com/questions/4037700/ggplot-boxplot-sort