12

Seems simple but i couldnt find a solution.

names(AllCoursesReg)
[1] "name"   "Course" "Status"

My Code

ggplot(AllCoursesReg, aes(Course, fill = Status)) + 
geom_bar(aes(order = Status), position = "dodge", colour = "black") + theme_bw()+
guides(fill = guide_legend(reverse = TRUE)) 

I just want the Registrants to be on the left not on the right. I have tried Order, level, factor, and it is not working

Thanks for your help.

enter image description here

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
Mohammad Zahrawy
  • 305
  • 1
  • 2
  • 9

2 Answers2

26

ggplot has been updated since this question, so here's an answer that utilises a new feature from ggplot2.

Just add position_dodge2(reverse = TRUE) to the position attribute. Using OP's code:

ggplot(AllCoursesReg, aes(Course, fill = Status)) + 
geom_bar(aes(order = Status), position=position_dodge2(reverse = TRUE), colour = "black") + theme_bw()+
guides(fill = guide_legend(reverse = TRUE)) 
JamesR
  • 613
  • 8
  • 15
5

You have to decide on the ordering of the levels of a factor. Here's an example from ?geom_bar.

# example from ?geom_bar
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")
# reorder cut using levels = rev(levels(cut))
ggplot(diamonds, aes(clarity, fill=factor(cut, levels = rev(levels(cut))))) + 
  geom_bar(position="dodge") + 
  scale_fill_discrete('cut') # change name back to cut
shadow
  • 21,823
  • 4
  • 63
  • 77
  • I am getting this, after applying it.Error in seq.default(h[1], h[2], length = n) : 'to' cannot be NA, NaN or infinite – Mohammad Zahrawy Mar 13 '15 at 15:33
  • 1
    If the reproducible example that I made does not fit your case, you should make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) yourself. Otherwise we cannot diagnose the problem. – shadow Mar 13 '15 at 17:57
  • 1
    my example is complete and easy to understand. – Mohammad Zahrawy Mar 13 '15 at 23:56