简体   繁体   English

按R中的平均值(而非中位数)对boxplot排序

[英]Sort boxplot by mean (and not median) in R

I have a simple boxplot, showing the distribution of a score for factor TYPE: 我有一个简单的boxplot,显示因子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 ). 我想用每个TYPE中SCORE的平均值对boxplot进行排序(在上面的例子中,顺序应该是a,c,b )。

Any hint? 任何提示?

This is a job for reorder() : 这是reorder()

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM