I want to sort the boxplots by the x-axis-values (hwy here) within every facet (class here). I tried 2 methods, but failed:
library(tidyverse); library(forcats)
mpg %>%
ggplot(aes(x = hwy, y = fct_reorder(trans, hwy, median))) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
mpg %>%
group_by(class) %>%
mutate(trans = fct_reorder(trans, hwy, median)) %>%
ungroup() %>%
ggplot(aes(x = hwy, y = trans)) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
What am I missing here?