1

My current data looks something like this df:

Year     Labels   Cost    Order
2006     A        1000       1
2007     B        2000       2
2008     B        2000       2
2009     C        3000       4
2010     NA        0         NA
etc.

As of now, my stacked chart is in the form of 1,2,4,NA I want to create a stacked chart of bar chart such that the bars are arranged in the following order (4,2,1,NA).

Ive tried:

ggplot(df[order(df$order,decreasing=T),],
             aes(x=x,y=y,fill=labels))+
  geom_bar(stat="identity")

But my order is the same. Anyone have any advice using dplyr?

Undo
  • 25,519
  • 37
  • 106
  • 129
R_abcdefg
  • 145
  • 1
  • 11
  • 1
    Possible duplicate of [ggplot2: Changing the order of stacks on a bar graph](https://stackoverflow.com/questions/34716245/ggplot2-changing-the-order-of-stacks-on-a-bar-graph) – www Sep 07 '17 at 02:05
  • you need to set the `levels` of x to the order you're looking for. Changing the order of rows don't affect the plotting order. – Jean Sep 07 '17 at 02:34
  • How do I set the levels of x? could you guide me on how to tweak my codes? thanks! – R_abcdefg Sep 07 '17 at 06:04
  • Previous comment for @waterling – R_abcdefg Sep 07 '17 at 06:11

1 Answers1

1

Here is the solution that might help you to change the levels of Order column:

df$Order <- factor(df$Order, levels = order(df$Order,decreasing=T))
Mal_a
  • 3,670
  • 1
  • 27
  • 60