1

In ggplot2 the histogram fill categories are ordered alphabetically. I would like to assign a custom order to my fill as such:

cust_order <- c("Goal", "Shot on target", "Shot blocked", "Wide shot")

This is different from reordering the colors as described here: How do you order the fill-colours within ggplot2 geom_bar.Code so far...

shot_df %>% filter(Team == "Team A") %>% 
  ggplot(., aes(x = xG, fill=Type)) +
  geom_histogram(binwidth = 0.10)

And the final (alphabetical) output: sample histogram

Thanks!

seansteele
  • 619
  • 3
  • 10
  • 1
    Could you please `dput(shot_df )` and paste the result in your question in order to help you? – Duck Aug 07 '20 at 16:27

1 Answers1

1

We can convert to factor with levels in the customer order vector and then it should work

shot_df$Type<- factor(shot_df$Type, levels = cust_order)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Why was this marked duplicate? The linked answer is not what I asked. It doesn't point to a solution that allows you to custom order your groupings. And the marked answer in the other one uses a deprecated function. How is that helpful? I don't know who marked it as a duplicate but @akrun should get credit and people should be able to see a concise answer to this specific question. – seansteele Aug 08 '20 at 04:49
  • @seansteele it is marked by somebody else as dupe. Not by me. thanks – akrun Aug 08 '20 at 16:17
  • 1
    @mrflick I have edited my question to make clear that is different from the one offered as already answering my question. Can you please reopen and allow akrun's answer stand? Thank you. – seansteele Aug 09 '20 at 04:08