0

I want to sort boxplot.
'left is tall box' and 'right is not tall'

ggplot(data = iris ,aes(x = Species,y=Sepal.Width)) +
  geom_violin(fill='gray')

I did this code, setosa:left and virginica:right.
but max(Sepal.Width) each Species, Versicolor is the least.
I want to sort setosa-virginica-versicolor reference to max(Sepal.Width).

h-y-jp
  • 199
  • 1
  • 8
  • The question has come up in earlier threads. Maybe https://stackoverflow.com/a/43221332/6503141 or https://stackoverflow.com/a/30494457/6503141 or the use of `xlim` in https://stackoverflow.com/a/3464682/6503141 or generally searching for ggplot order boxplot should help – Bernhard Oct 17 '20 at 12:44

1 Answers1

2

Maybe you are looking for this. You can use reorder() with the option FUN enabled. In this case you can use max, if I understand you correctly. I have included two options:

library(ggplot2)
library(dplyr)
#Code 1
ggplot(data = iris ,aes(x = reorder(Species,Sepal.Width,FUN = max),y=Sepal.Width)) +
  geom_violin(fill='gray')+xlab('Species')

Output:

enter image description here

Or this:

#Code 2
ggplot(data = iris ,aes(x = reorder(Species,-Sepal.Width,FUN = max),y=Sepal.Width)) +
  geom_violin(fill='gray')+xlab('Species')

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84