title: reorder x-axis variables by sorting a subset of the data
body: <p>I am trying to make a stacked bar chart and I would like to reorder the variables on the x-axis based on the data from a single category. In the example below there are three x values each of which have values corresponding to three categories. How would it be possible to plot the graph while sorting the name values for increasing abundance of "bb". </p>
  
  <p>Although this question is similar to other questions about <a href="https://stackoverflow.com/questions/5916779/sorting-of-categorical-variables-in-ggplot?rq=1">reordering</a> <a href="https://stackoverflow.com/questions/14033342/reorder-a-factor-by-a-numeric-variable?rq=1">categorical</a> <a href="https://stackoverflow.com/questions/8186436/order-stacked-bar-graph-in-ggplot/8186560#8186560">variables</a> the difference here is that the ordering is based on a subset of one column's data. Any suggestions appreciated.</p>
  
  <pre><code>#create the dataframe
  name = c('a', 'a', 'a', 'b', 'b', 'b','c','c','c') 
  cat = c("aa", "bb", "cc", "aa", "bb", "cc","aa", "bb", "cc") 
  percent = c( 5 , 5, 90, 40, 40 , 20, 90,5,5) 
  df = data.frame(name, cat, percent)
  
  #stacked barchart with default ordering   
  ggplot(df, aes(x=name,y=percent, fill=cat)) + geom_bar(position="fill")
  
  #I'm looking to reorder the x-axis by the `percent` values for category "bb"
  vals = df[ df$cat == 'bb', ]                     #subset
  xvals = vals[with(vals, order(percent)), ]$name  #get values
  ggplot(df, aes(x =reorder(name, xvals ), y = percent, fill=cat])) + geom_bar(position="fill") #order with new values
  </code></pre>