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
How can I make it so that the variable with the most variation would be on the left of this plot?
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).

