1

I have made a bar chart: enter image description here

The category label on the right side doesn't follow the numerical order. My code is as following:

ggplot(dat3, aes(variable, value, fill = AnnualCompensation, color = AnnualCompensation)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_y_continuous(name = "Number of Workers", labels = comma)

How can I reorder the category on the right side and make the bars in the chart follow the order AnnualCompensation?

And below is the dataset that I used: enter image description here

Scransom
  • 2,857
  • 2
  • 28
  • 46
T.T
  • 27
  • 1
  • 4
  • 1
    You need to change the order of the factor levels of `AnnualCompensation`. For example, `dat3$AnnualCompensation )`. Or set the levels directly `levels(dat3$AnnualCompensation) `. Or use `relevel`. – Maurits Evers Oct 27 '17 at 00:19
  • 1
    Might be worth doing a google search on the `fct_reorder()` function in the `forcats` package, I've found that function to be be particularly useful for exactly this situation. Not always a fan of adding new package dependencies, but I have yet to find a base R way to accomplish the same end in as few keystrokes. – Matt Summersgill Oct 27 '17 at 02:28

1 Answers1

0

You must reorder your levels in R- Most likely your levels are following that order. use levels() to see which is assigned to level 1,2,...

Miha
  • 2,439
  • 2
  • 17
  • 29
RomRom
  • 294
  • 1
  • 10