5
When using the command fct_reorder(class, hwy, .fun = median) below, I asked for the variable levels class were reordered according to median variable hwy:
library(tidyverse)
ggplot(mpg, aes(x = fct_reorder(class, hwy, .fun = median), y = hwy)) +
geom_boxplot() +
facet_wrap(~ year) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "class")

Created on 2021-08-09 by the reprex package (v2.0.1)
However, the levels of class were reordered according to the general medians of class, and not of class for year. That is, the panel referring to 1999 appears, by chance, as desire, but the panel referring to 2008 does not. The order of Compact and midsize is altered.
How can I make fct_reorder understand that you would like to reorder your levels by more than one variable in sequence?

The idea of creating a new factor by pasting class and year is very good. I liked this strategy.
– Marcus Nunes