Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: How to order boxplots by variance?

Is it possible to order box plots in R by their variance?

rm(list = ls())
library(datasets)
library(ggplot2)

data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))


p10 <- ggplot(airquality, aes(x = Month, y = Ozone)) +
        geom_boxplot()
p10

Box-Plot

How can I make it so that the variable with the most variation would be on the left of this plot?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help at all would be greatly appreciated!

>Solution :

With reorder you could do:

library(ggplot2)

data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))


ggplot(airquality, aes(x = reorder(Month, Ozone, function(x) -var(x, na.rm = TRUE)), y = Ozone)) +
  geom_boxplot()
#> Warning: Removed 37 rows containing non-finite values (stat_boxplot).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading