boxplot:按每个组的子集的平均值排序组 [英] boxplot: order groups by the mean of a subset of each group

查看:903
本文介绍了boxplot:按每个组的子集的平均值排序组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们考虑这些数据:
$ b $ pre $ df = data.frame('score'= round(runif(15,1, 10)),
'group'= paste0(a,rep(c(1,2,3),each = 5)),
'category'= rep(c(big ,大,大,大,小),3))

我想用 ggplot2 来绘制这些数据的箱型图。我想要的是:boxplot(分数〜组),但是根据每个组的大个体的平均值排列盒形图。



我无法以简单的方式找出它,而无需创建新变量。好的使用Dplyr。感谢。

解决方案

我不知道这是否符合简单的方法,我个人觉得很简单, code> dplyr 查找方法:

 #查找每个组的
library(dplyr)
意味着< -
df%>%
#因为您只需要类别为'big'的$ f
过滤器(category = ='big')%>%
#使用与ggplot中相同的组
group_by(group)%>%
#calculate the means
summarize(mean =平均(分数))

#根据平均值的顺序对组进行排序
myorder< - 表示$ group [order(means $ mean)]

在这种情况下,订单为:

 > myorder 
[1] a1 a2 a3

为了排列箱形图的顺序,到上面你只需要做:

  library(ggplot2)
ggplot(df,aes(group,score ))+
geom_boxplot()+
#您只需要使用scale_x_discrete和limits参数
#来传递箱线图的出现顺序的详细信息
#in this如果订单是myorders vector
scale_x_discrete(limits = myorder)

就是这样。




Let's consider this data:

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))

I would like to plot boxplots of this data with ggplot2. What i want is: boxplot(score~group), but with the boxplots arranged according to the mean of the "big" individuals of each group.

I can't figure it out in a simple way, without creating new variables. OK to use Dplyr. Thanks.

解决方案

I don't know if this qualifies as a simple way, I personally find it simple, but I use dplyr to find the means:

#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)]

In this case the order is:

> myorder
[1] a1 a2 a3

In order to arrange the order of the boxplots according to the above you just need to do:

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)

And that's it.

这篇关于boxplot:按每个组的子集的平均值排序组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆