35

Data example from dbv:

  gender Sektion
1      m       5
2      m       5
3      w      3B
4      w      3B
5      w      3B
6      m       4

I have the following plot:

Sekplot <- ggplot(dbv,aes(x=Sektion,
                          fill=factor(gender),
                          stat="bin", 
                          label = paste(round((..count..)/sum(..count..)*100), "%"))) 
Sekplot <- Sekplot + geom_bar(position="fill")
Sekplot <- Sekplot + scale_y_continuous(labels = percent)
Sekplot <- Sekplot + labs(title = "test")
Sekplot <- Sekplot + scale_fill_discrete(name="test", breaks=c("m", "w", "k.A."), labels=c("m", "w", "k.A."))
Sekplot <- Sekplot + geom_hline(aes(yintercept = ges, linetype = "test"), colour = "black", size = 0.75, show_guide = T)
Sekplot <- last_plot() + coord_flip()
Sekplot <- Sekplot + guides(colour = guide_legend(override.aes = list(linetype = 0 )), 
                                    fill = guide_legend(override.aes = list(linetype = 0 )), 
                                    shape = guide_legend(override.aes = list(linetype = 0 )), 
                                    linetype = guide_legend()) + theme(legend.title=element_blank())

Sekplot 

Output: Plot with y-axis in wrong order

Plot with y-axis in wrong order

How can I reverse the order of the "Sektion" axis? I'd like to have one on top and 8 at the bottom.

I tried, according to groupA$Date <- factor(groupA$Date, levels=rev(unique(groupA$Date))):

Sekplot <- last_plot() + coord_flip() + scale_x_reverse()

in several flavor, but couldn't find the right way.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
CH_
  • 685
  • 1
  • 7
  • 18
  • 1
    You need something like `scale_x_discrete(limits = rev(levels(dat$Sektion)))` - see [this answer](http://stackoverflow.com/a/7310754/2461552) – aosmith Dec 11 '15 at 21:48
  • Thanks a lot, it worked like a charm! I cannot mark this question as solved, because the answer is in a comment. – CH_ Dec 14 '15 at 13:45
  • 2
    I hadn't put it as an answer because I was trying to find duplicates. I haven't found a really good match, though, so will add as answer. – aosmith Dec 14 '15 at 15:40

2 Answers2

60

You can add scale_x_discrete with the limits argument to do this. You could simply write out the limits in the order you want, but that gets complicated when you have many factor levels.

Instead, you can pull the levels of the factor from your dataset and take advantage of rev to put them in reverse order.

It would look like:

scale_x_discrete(limits = rev(levels(dbv$Sektion)))

2022 edit by @slhck

Adding in example using forcats::fct_rev() function to set the levels of the factor in reverse order. You can either make this change in the dataset or use directly when mapping your x variable as below.

ggplot(dbv, aes(x = forcats::fct_rev(Sektion),
               fill = factor(gender),
               stat = "bin", 
               label = paste(round((..count..)/sum(..count..)*100), "%")
   )
)
...
aosmith
  • 34,856
  • 9
  • 84
  • 118
4

As suggested by @sindri_baldur, while you're specifying the aesthetics, add fct_rev() like so ggplot(dbv,aes(x=fct_rev(Sektion), fill=factor(gender),stat="bin", label = paste(round((..count..)/sum(..count..)*100), "%")))

That should be all you need ... remove the scale_x_discrete() from your code.

Hammao
  • 801
  • 1
  • 9
  • 28