Search code examples
rggplot2dplyrstringrforcats

How to remove facet values from y-axis-values while using reorder_within from the tidytext to order boxplots within every facet?


How can I remove facet values from y-axis-values while using reorder_within from the tidytext to order boxplots within every facet?

Here is my code:

library(tidyverse); library(tidytext)

mpg %>% 
  ggplot(aes(x = hwy, y = reorder_within(trans, hwy, class, median))) + 
  geom_boxplot() +
  facet_wrap(~class, scales = "free_y")

And these are the facet values I want to remove.

enter image description here


Solution

  • You need to add scale_y_reordered():

    library(tidyverse); library(tidytext)
    
    mpg %>% 
      ggplot(aes(x = hwy, y = reorder_within(trans, hwy, class, median))) + 
      geom_boxplot() +
      scale_y_reordered() +
      facet_wrap(~class, scales = "free_y")