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

后端 未结 1 1627
梦毁少年i
梦毁少年i 2021-01-23 13:02

Let\'s consider this data:

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


        
相关标签:
1条回答
  • 2021-01-23 13:39

    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.

    enter image description here

    0 讨论(0)
提交回复
热议问题