title: how to change the order of a discrete x scale in ggplot with unbalenced data (NA on the x-axis)
body: <p>I am trying to do a faceted ggplot2 plot of error bars, but failing. Data is much like this...</p>
  
  <pre><code>library(ggplot2)
  library(dplyr)
  
  df0 &lt;- iris %&gt;%
    group_by(Species) %&gt;%
    mutate(long_sepal = ifelse(Sepal.Length &gt; mean(Sepal.Length), 
           yes = "long", no = "short")) %&gt;%
    group_by(Species, long_sepal) %&gt;%
    mutate(petal_rank = order(Petal.Width)) %&gt;%
    filter(petal_rank &lt;= 5) %&gt;%
    mutate(petal_rank = factor(petal_rank))
  
  &gt; df0
  # Source: local data frame [30 x 7]
  # Groups: Species, long_sepal [6]
  # 
  #    Sepal.Length Sepal.Width Petal.Length Petal.Width Species long_sepal petal_rank
  #           (dbl)       (dbl)        (dbl)       (dbl)  (fctr)      (chr)      (int)
  # 1           5.4         3.9          1.7         0.4  setosa       long          1
  # 2           4.6         3.4          1.4         0.3  setosa      short          1
  # 3           5.0         3.4          1.5         0.2  setosa      short          2
  # 4           4.4         2.9          1.4         0.2  setosa      short          3
  # 5           4.9         3.1          1.5         0.1  setosa      short          4
  # 6           5.4         3.7          1.5         0.2  setosa       long          3
  # 7           5.8         4.0          1.2         0.2  setosa       long          4
  # 8           5.2         4.1          1.5         0.1  setosa       long          2
  # 9           5.5         4.2          1.4         0.2  setosa       long          5
  # 10          4.5         2.3          1.3         0.3  setosa      short          5
  # ..          ...         ...          ...         ...     ...        ...        ...
  </code></pre>
  
  <p>I could use the <code>scale_x_discrete</code> function to order the discrete axis as I require (i.e. <code>short</code> first, uncomment in the plot code below). However, my data has some groups where there is no classification for the variable on the x-axis, so more like:</p>
  
  <pre><code>df0 &lt;- df0 %&gt;%
    ungroup() %&gt;%
    mutate(long_sepal = ifelse(Species != "virginica", yes = long_sepal, no = NA),
           long_sepal = factor(long_sepal))
  </code></pre>
  
  <p>The plot code gives either gives an <code>NA</code> on the x-axis, which I want to avoid...</p>
  
  <pre><code>ggplot(data = df0, 
         aes(x = long_sepal, y = Petal.Width, group = factor(petal_rank),
             ymin = Petal.Width-0.05, 
             ymax = Petal.Width+0.05)) + 
    geom_pointrange(position = position_dodge(width = 0.4)) +
    facet_wrap(~ Species, scales = "free") # + scale_x_discrete(limits=c("short", "long"))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FoVe1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FoVe1.png" alt="enter image description here"></a></p>
  
  <p>... or an error message when I uncomment the <code>scale_x_discrete</code> function; </p>
  
  <pre><code>Error in if (zero_range(from) || zero_range(to)) { : 
    missing value where TRUE/FALSE needed
  </code></pre>
  
  <p>Is there anyway to do the plot with a specified order of the discrete values and without an <code>NA</code> label in the facet with no categorisation for the x-axis?</p>