Sorting a boxplot based on median value

后端 未结 3 1492
滥情空心
滥情空心 2020-12-24 05:54

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)

This would g

相关标签:
3条回答
  • 2020-12-24 06:30

    Yes, that is the idea:

    > set.seed(42)                     # fix seed       
    > DF <- data.frame(type=sample(LETTERS[1:5], 100, replace=TRUE), 
    +                  cost=rnorm(100)) 
    >
    > boxplot(cost ~ type, data=DF)    # not ordered by median
    >
    > # compute index of ordered 'cost factor' and reassign          
    > oind <- order(as.numeric(by(DF$cost, DF$type, median)))    
    > DF$type <- ordered(DF$type, levels=levels(DF$type)[oind])   
    >
    > boxplot(cost ~ type, data=DF)    # now it is ordered by median
    
    0 讨论(0)
  • 2020-12-24 06:35

    Beware of missing values, you have to add na.rm = TRUE for it to work. If not, the code simply doesn't work. It took me hours to found that out.

      bymedian <- with(InsectSprays, reorder(spray, -count, median, **na.rm = TRUE**)
      boxplot(count ~ bymedian, data = InsectSprays,
              xlab = "Type of spray", ylab = "Insect count",
              main = "InsectSprays data", varwidth = TRUE,
              col = "lightgray")
    
    0 讨论(0)
  • 2020-12-24 06:45

    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")
    
    0 讨论(0)
提交回复
热议问题