Sorting a boxplot based on median value

speciousfool picture speciousfool · Sep 22, 2010 · Viewed 31.5k times · Source

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 give me some boxplots were cost is shown on the y axis and the type category is visible on the x-axis:

-----     -----
  |         |
 [ ]        |
  |        [ ]
  |         |
-----     -----
  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.

Answer

Joshua Ulrich picture Joshua Ulrich · Sep 22, 2010

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