1

When I try to reorder y-axis based on the value of x and then facet by another variable, the ordering is lost.

Below an example

library("ggplot2")

# dummy data_frame
df <- data_frame(subj = as.factor(rep(c("Adam", "Brenna", "Charlie"), each = 3)),
                 type = as.factor(rep(c("A","B", "C"), 3)),
                 score = c(50, 0, 100, rep(c(100, 50, 0), 2)))

# A tibble: 9 x 3
subj    type  score
<fct>   <fct> <dbl>
1 Adam    A        50
2 Adam    B         0
3 Adam    C       100
4 Brenna  A       100
5 Brenna  B        50

Plot without faceting and with only one subj works well as expected

df %>% 
  filter(subj == "Adam") %>% 
  ggplot(aes(score, reorder(type, score))) +
  geom_point()

plot without faceting

and here after faceting by subj

   + facet_grid(subj ~., scales = "free_y")

faceting

I did try to reorder also by subj and other approaches for reorder after faceting but nothing. I can definitely come with a work around (i.e. create 3 single plots and then combine them together with gridExtra) but a more elegant way should be available and it seems that I am clearly missing some important details.

Thanks! :)

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
c1au61o_HH
  • 867
  • 7
  • 14

0 Answers0