title: Troubles with ggplot and geom_bar
body: <p>Here the updated example:</p>
  
  <pre><code>df &lt;- data.frame(a=rep(c("A","B"),each=10),
                   b=rep(rep(c("C","D"),each=5),2),
                   c=c(sample(letters[1:5]), sample(letters[6:10]),           
                       sample(letters[1:5]), sample(letters[6:10])),
                   d=c(0.10,0.18,0.34,0.35,0.59,0.16,0.38,0.40,0.53,0.58,
                       0.37,0.62,0.83,1.46,-0.91,-0.79,-0.52,-0.43,-0.01,0.34))
  
  &gt; df
     a b c     d
  1  A C b  0.10
  2  A C e  0.18
  3  A C a  0.34
  4  A C c  0.35
  5  A C d  0.59
  6  A D i  0.16
  7  A D j  0.38
  8  A D h  0.40
  9  A D f  0.53
  10 A D g  0.58
  11 B C e  0.37
  12 B C d  0.62
  13 B C a  0.83
  14 B C c  1.46
  15 B C b -0.91
  16 B D f -0.79
  17 B D i -0.52
  18 B D h -0.43
  19 B D j -0.01
  20 B D g  0.34
  </code></pre>
  
  <p>If you look closely, you will see that column d is ordered within column b always from smallest to largest.</p>
  
  <p>The first plot is how I would like to have the plot apart from the fact, that the bars displayed are not in the order of d. So the bars do not appear from smallest to largest:</p>
  
  <pre><code>p &lt;- ggplot(df, aes(x=c, y=d, fill=b, stat="identity")) +
  
  facet_grid(. ~ a) +
  
  geom_bar()  
  
  print(p)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/9HqFF.png" alt="barplot1"></p>
  
  <p>This is because column c is a factor and the factors are apparently not ordered in the same order as column d is. So I did the following:</p>
  
  <pre><code>df$c &lt;- paste(1:nrow(df), df$c, sep="_")
  
  df$c &lt;- factor(df$c, levels = unfactor(df$c))
  
  p &lt;- ggplot(df, aes(x=c, y=d, fill=b, stat="identity")) +
  
              facet_grid(. ~ a) +
  
              geom_bar()  
  
  print(p)
  </code></pre>
  
  <p>produces the following plot:</p>
  
  <p><img src="https://i.stack.imgur.com/KAOjD.png" alt="enter image description here"></p>
  
  <p>Here the order is correct. However, as you can see I created unique factors I get those spaces for the ones not present in A and B respectively.</p>
  
  <p>How can I sort that out?</p>