Sort boxplot by mean (and not median) in R

拥有回忆 提交于 2019-11-27 17:55:14

问题


I have a simple boxplot, showing the distribution of a score for factor TYPE:

myDataFrame = data.frame( TYPE=c("a","a","b","b","c","c"), SCORE=c(1,1,2,3,2,1) )
boxplot( SCORE~TYPE, data=myDataFrame )

The various types are shown in the order they have in the data frame. I'd like to sort the boxplot by the mean of SCORE in each TYPE (in the example above, the order should be a,c,b).

Any hint?


回答1:


This is a job for reorder():

myDataFrame$TYPE <- with(myDataFrame, reorder(TYPE, SCORE, mean))
boxplot( SCORE~TYPE, data=myDataFrame )



来源:https://stackoverflow.com/questions/9741600/sort-boxplot-by-mean-and-not-median-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!