0

I am plotting a simple barplot using ggplot2 and it is oddly re-ordering the location of the 0 value. Here is the code and plot below:

x <- c("Oct-Nov","Nov-Dec", "Dec-Jan", "Jan-Feb", "Feb-March")
zz <- c(0.6363636,  0.1666667,  0.0000000, -0.2857143 , 0.6666667)  
qplot(x, zz, geom = "bar", stat = "identity")

enter image description here

Any ideas on why this is? Thanks

coding_heart
  • 1,245
  • 3
  • 25
  • 46
  • ggplot2 orders alphabetically by default. This is probably a duplicate of [this](http://stackoverflow.com/q/5208679/324364) question. – joran Oct 29 '15 at 16:59

1 Answers1

2

ggplot is sorting your vector alphabetically. To avoid this behavior, you should transform your vector in a factor before plotting.

x <- c("Oct-Nov","Nov-Dec", "Dec-Jan", "Jan-Feb", "Feb-March")
x<-factor(x,levels=unique(x))
zz <- c(0.6363636,  0.1666667,  0.1000000, -0.2857143 , 0.6666667)  
qplot(x, zz, geom = "bar", stat = "identity")
LeCoco
  • 61
  • 1
  • 1
  • 5