title: Order of factor levels in facet_wrap
body: <p>I'd like to produce a facet_wrap where the order of factors within facets is
  based on the one of the column factor order. The heart of the problem is each group has duplicated factor levels and when I do plotting only one factor level is ordered correctly in the <code>facet_wrap</code>. (See the graph below)</p>
  
  <p>I try to order factor levels in each group and each factor level should be ordered correctly inside of each facet.</p>
  
  <p>Here is my attempt </p>
  
  <pre><code>df_pattern&lt;- data.frame(address = rep(rep(LETTERS[1:3]),3)) 
  
  df_TP &lt;- data.frame(No=rep(seq(1:3)),
                      clas=c("Good","Bad","Ugly"),stringsAsFactors = F)
  
  set.seed(12)
  df_ex &lt;- df_pattern%&gt;%
    mutate(No=rep(seq(1:3),each=3))%&gt;%
    left_join(df_TP)%&gt;%
    mutate(clas=sample(clas))%&gt;%
    group_by(No)
  
  #      address    No  clas
  #       &lt;fctr&gt; &lt;int&gt; &lt;chr&gt;
  #    1       A     1  Good
  #    2       B     1  Ugly
  #    3       C     1  Ugly
  #    4       A     2  Good
  #    5       B     2  Ugly
  #    6       C     2   Bad
  #    7       A     3   Bad
  #    8       B     3   Bad
  #    9       C     3  Good
  </code></pre>
  
  <p>Now lets try to sort address levels according to user defined clas column order</p>
  
  <pre><code>set.seed(12)
  df_ex &lt;- df_pattern%&gt;%
    mutate(No=rep(seq(1:3),each=3))%&gt;%
    left_join(df_TP)%&gt;%
    mutate(clas=sample(clas))%&gt;%
    group_by(No)%&gt;%
    mutate(clas=factor(clas,levels=c("Good","Bad","Ugly")))%&gt;%
    mutate(address=factor(address,levels=unique(address[order(clas)])))%&gt;%
    mutate(address=as.character(address))%&gt;%
    arrange(No,clas) 
  
        address    No  clas
  #       &lt;fctr&gt; &lt;int&gt; &lt;ord&gt;
  #    1       A     1  Good
  #    2       B     1  Ugly
  #    3       C     1  Ugly
  #    4       A     2  Good
  #    5       C     2   Bad
  #    6       B     2  Ugly
  #    7       C     3  Good
  #    8       A     3   Bad
  #    9       B     3   Bad
  </code></pre>
  
  <p>As you can see only the <code>No=1</code> group ordered correctly in the plot. Maybe this because only one factor level in the data set. </p>
  
  <pre><code>&gt; levels(df_ex$address)
  [1] "A" "B" "C"
  </code></pre>
  
  <p>How can we order factor levels in each group and show them in the facet_wrap? according to <code>clas</code> levels in each <code>facet_wrap</code>?</p>
  
  <p>Thanks!</p>
  
  <p>ggplot code</p>
  
  <pre><code>ggplot(df_ex, aes(x=address,y="",fill=clas)) + #x axis bias voltage dependence
    geom_tile() + 
    scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
    facet_wrap(~No,ncol=1,scales = "free_x")+
    theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
          axis.title.y = element_text(face="bold",size = 20, colour = "black"),
          axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
          strip.text = element_text(size=26, face="bold"),
          strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
          plot.title=element_text(face="bold",color="red",size=14),
          legend.title = element_text(colour="black", size=26,face="bold"),
          legend.text = element_text(colour="black", size=18))+
    labs(x = "address",y = "")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LWcYE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LWcYE.png" alt="enter image description here"></a></p>