根据中位数对箱形图进行排序 [英] Sorting a boxplot based on median value
问题描述
我想使用R进行一系列箱形图,这些箱形图按中值排序。假设然后执行:
I'd like to use R to make a series of boxplots which are sorted by median value. Suppose then I execute:
boxplot(cost ~ type)
如果在y轴上显示了成本并且在x轴上显示了类型类别,这会给我一些盒形图:
This would give me some boxplots were cost is shown on the y axis and the type category is visible on the x-axis:
----- -----
| |
[ ] |
| [ ]
| |
----- -----
A B
我想要的是根据最高和最低中位数排序的箱线图。我的怀疑是,我需要做的是更改类型(A或B)的标签以数字方式指示哪个是最低和最高中值,但是我想知道是否有更聪明的方法来解决此问题。
However, what I'd like is the boxplot figures sorted from highest to lowest median value. My suspicion is that what I need to do is change the labels of the type (A or B) to numerically indicate which is the lowest and highest median value, but I wonder if there is a more clever way to solve the problem.
推荐答案
签出?reorder
。该示例似乎是您想要的,但以相反的顺序排序。我在下面的第一行中更改了-计数
来按所需顺序进行排序。
Check out ?reorder
. The example seems to be what you want, but sorted in the opposite order. I changed -count
in the first line below to sort in the order you want.
bymedian <- with(InsectSprays, reorder(spray, -count, median))
boxplot(count ~ bymedian, data = InsectSprays,
xlab = "Type of spray", ylab = "Insect count",
main = "InsectSprays data", varwidth = TRUE,
col = "lightgray")
这篇关于根据中位数对箱形图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!