箱线图:按每个组的子集的平均值对组进行排序

boxplot: order groups by the mean of a subset of each group

让我们考虑一下这个数据:

df = data.frame('score'=round(runif(15, 1, 10)),
                'group'=paste0("a",rep(c(1,2,3),each=5)),
                'category'=rep(c("big", "big", "big", "big", "small"), 3))

我想用 ggplot2 绘制此数据的箱线图。我想要的是:箱线图(分数~组),但箱线图是根据每个组的 "big" 个人的平均值排列的。

如果不创建新变量,我无法用简单的方法解决。可以使用 Dplyr。谢谢

不知道这个算不算简单方法,我个人觉得简单,但是我用dplyr找方法:

#find the means for each group
library(dplyr)
means <-
df %>%
  #filter out small since you only need category equal to 'big'
  filter(category=='big') %>%
  #use the same groups as in the ggplot
  group_by(group) %>%
  #calculate the means
  summarise(mean = mean(score))

#order the groups according to the order of the means
myorder <- means$group[order(means$mean)]

在这种情况下,顺序是:

> myorder
[1] a1 a2 a3

为了按照上面的顺序排列箱线图你只需要做:

library(ggplot2)
ggplot(df, aes(group, score)) +
  geom_boxplot() +
  #you just need to use scale_x_discrete with the limits argument
  #to pass in details of the order of appearance for the boxplots
  #in this case the order is the myorders vector
  scale_x_discrete(limits=myorder)

就是这样。