title: R: Sorting factor by level frequency and plotting
body: <p>I have a <em>data.frame</em> with several factors such as:</p>
  
  <pre><code>df&lt;-data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), 1000, replace=TRUE)))
  </code></pre>
  
  <p>with</p>
  
  <pre><code>summary(df$Var1)
   AB  BC  CD  DE  EF 
  209 195 178 221 197
  </code></pre>
  
  <p>I want to plot the frequency of the levels of each factor in the <em>data.frame</em> as follows:</p>
  
  <pre><code>ggplot(df, aes(x=factor(1), fill=factor(Var1)))+
         geom_bar(width=1, colour="black")+
         coord_polar(theta="y")+
         theme_void()
  </code></pre>
  
  <p>However, the order of the levels is alphabetically and not by frequency. Using <em>count</em> from <em>library(plyr)</em> I can create a new data.frame that gives me the frequency of each level:</p>
  
  <pre><code>df_count &lt;-count(df, "Var1")
  Var1 freq
  1   AB  209
  2   BC  195
  3   CD  178
  4   DE  221
  5   EF  197
  </code></pre>
  
  <p>Which I can then reorder using </p>
  
  <pre><code>df_count$Var1&lt;-factor(df_count$Var1, levels=df_count$Var1[order(df_count$freq, decreasing=TRUE)])
  </code></pre>
  
  <p>which when plotted give me what I want, the sorted frequency of each level.</p>
  
  <p>1.) Is this the most elegant solution? It gives me an extra data.frame for each factor/column in my original data.frame, and I feel there must be a simpler way. </p>
  
  <p>2.) When plotting, how can I rename the legend lables and ensure they are allocated the right factor level? If I use </p>
  
  <pre><code>scale_fill_manual(labels=c("Name of AB", "Name of BC", "Name of CD", "Name of DE","Name of EF"))
  </code></pre>
  
  <p>the labels do not relate to the right level. Here the first entry in the legend will be "DE" as it is the level with the highest frequency but the label will say "Name of AB" as defined in <em>scale_fill_manual.</em>  I could check the order of the labels manually each time but there must be an automatic way? </p>