title: Remove unused factor levels from a ggplot bar plot
body: <p>I want to do the opposite of <a href="https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot">this question</a>, and sort of the opposite of <a href="https://stackoverflow.com/questions/10002627/ggplot2-0-9-0-automatically-dropping-unused-factor-levels-from-plot-legend">this question</a>, though that's about legends, not the plot itself.</p>
  
  <p>The other SO questions seem to be asking about how to <em>keep</em> unused factor levels. I'd actually like mine removed. I have several name variables and several columns (wide format) of variable attributes that I'm using to create numerous bar plots. Here's a reproducible example:</p>
  
  <pre><code>library(ggplot2)
  df &lt;- data.frame(name=c("A","B","C"), var1=c(1,NA,2),var2=c(3,4,5))
  ggplot(df, aes(x=name,y=var1)) + geom_bar()
  </code></pre>
  
  <p>I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/iH15f.png" alt="enter image description here"></p>
  
  <p>I'd like only the names that have corresponding var<sub>n</sub>'s show up in my bar plot (as in, there would be no empty space for B).</p>
  
  <p>Reusing the base plot code will be quite easy if I can simply change my output file name and <code>y=var</code> bit. I'd like not have to subset my data frame just to use droplevels on the result for each plot if possible!</p>
  
  <hr>
  
  <p><strong>Update based on the <code>na.omit()</code> suggestion</strong></p>
  
  <p>Consider a revised data set:</p>
  
  <pre><code>library(ggplot2)
  df &lt;- data.frame(name=c("A","B","C"), var1=c(1,NA,2),var2=c(3,4,5), var3=c(NA,6,7))
  ggplot(df, aes(x=name,y=var1)) + geom_bar()
  </code></pre>
  
  <p>I need to use <code>na.omit()</code> for plotting <code>var1</code> because there's an NA present. But since na.omit makes sure values are present for <em>all columns</em>, the plot removes <code>A</code> as well since it has an NA in <code>var3</code>. This is more analogous to my data. I have 15 total responses with NAs peppered about. I only want to remove factor levels that don't have values for the <em>current</em> plotted y vector, not that have NAs in <em>any</em> vector in the whole data frame.</p>