title: Removing NAs from ggplot facet labels
body: <p>I have a <code>data.frame</code> I'd like to <code>ggplot</code> with <code>facets</code>:</p>
  
  <pre><code>library(dplyr)
  
  set.seed(1)
  samples &lt;- c("mt1.2m_1","mt1.2m_2","mt1.4m_1","mt1.4m_2","mt1.4m_3","mt1.4m_4","mt1.4m_5","mt1.6m_1","mt1.6m_2","mt1.6m_3","mt1.6m_4","mt1.6m_5",
               "mt2.2m_1","mt2.2m_2","mt2.4m_1","mt2.4m_2","mt2.6m_1","mt2.6m_2",
               "wt.2m_1","wt.2m_2","wt.2m_3","wt.2m_4","wt.2m_1","wt.4m_2","wt.4m_3","wt.4m_4","wt.6m_1","wt.6m_2","wt.6m_3","wt.6m_4")
  val &lt;- rnorm(30)
  
  df &lt;- data.frame(sample=samples,val=rnorm(length(samples)),gt2m=NA,gt4m=NA,gt6m=NA,stringsAsFactors=F)
  df$replicate &lt;- as.integer(gsub("^.*_","",df$sample,perl=T))
  df$gt2m[which(grepl("2m_",df$sample))] &lt;- gsub("_\\d+$","",df$sample[which(grepl("2m_",df$sample))],perl=T)
  df$gt4m[which(grepl("4m_",df$sample))] &lt;- gsub("_\\d+$","",df$sample[which(grepl("4m_",df$sample))],perl=T)
  df$gt6m[which(grepl("6m_",df$sample))] &lt;- gsub("_\\d+$","",df$sample[which(grepl("6m_",df$sample))],perl=T)
  
  df$gt2m &lt;- factor(df$gt2m,levels=c("mt1.2m","mt2.2m","wt.2m"))
  df$gt4m &lt;- factor(df$gt4m,levels=c("mt1.4m","mt2.4m","wt.4m"))
  df$gt6m &lt;- factor(df$gt6m,levels=c("mt1.6m","mt2.6m","wt.6m"))
  
  #add error bar values
  val.error &lt;- 0.05*mean(abs(df$val))
  df$min.val &lt;- df$val-3*val.error
  df$low.val &lt;- df$val-val.error
  df$max.val &lt;- df$val+3*val.error
  df$high.val &lt;- df$val+val.error
  
  #add a factor to color by
  df$col.fill &lt;- factor(apply(dplyr::select(df,gt2m,gt4m,gt6m),1,function(x) x[which(!is.na(x))]))
  </code></pre>
  
  <p>I want to facet the plot by the <code>gt2m</code>, <code>gt4m</code>, and <code>gt6m</code> columns - which are sparse. I define a <code>forumla</code> for that:</p>
  
  <pre><code>facet.formula &lt;- as.formula("~gt2m+gt4m+gt6m")
  </code></pre>
  
  <p>Here's how I'm trying to plot this:</p>
  
  <pre><code>library(ggplot2)
  
  ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
    geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
    facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
      labels &lt;- lapply(labels, as.character)
      list(do.call(paste, c(labels, list(sep = ":"))))})
  </code></pre>
  
  <p>Which gives me:
  <a href="https://i.stack.imgur.com/XSdvd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XSdvd.png" alt="enter image description here"></a></p>
  
  <p>That's almost perfect except for the <code>NA</code>s in the <code>facet</code> labels.</p>
  
  <p>I tried:</p>
  
  <pre><code>ggplot(df,aes(x=replicate,ymin=min.val,lower=low.val,middle=val,upper=high.val,ymax=max.val,col=col.fill,fill=col.fill))+
    geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
    facet_wrap(scales="free_x",facet.formula,ncol=length(levels(df$col.fill)),labeller = function (labels) {
      labels &lt;- lapply(labels, as.character)
      list(do.call(paste, c(labels[which(labels != "NA")], list(sep = ":"))))})
  </code></pre>
  
  <p>But that gives the same result.</p>
  
  <p>Any idea how to delete these <code>NA</code>s from the <code>facet</code> labels?</p>