title: ggplot - ordering x-axis labels by multiple columns
body: <p>with a table like below</p>
  
  <pre><code>df &lt;- read.table(textConnection("
  tier make model sales
  entry Toyota Yeti 10000
  entry Honda Jazz 8000
  entry Nissan Sunny 5000
  entry Honda Amaze 4000
  entry Toyota Model10 3500
  entry Nissan Beat 2000
  Mid Honda Civic 4000
  Mid Toyota Corolla 3000
  Mid Honda Accord 2500
  Mid Nissan Xtrail 2200
  Mid Toyota Camry 1800
  Mid Nissan Moon 800
  "), header = TRUE)
  
  &gt; df
      tier   make   model sales
  1  entry Toyota    Yeti 10000
  2  entry  Honda    Jazz  8000
  3  entry Nissan   Sunny  5000
  4  entry  Honda   Amaze  4000
  5  entry Toyota Model10  3500
  6  entry Nissan    Beat  2000
  7    Mid  Honda   Civic  4000
  8    Mid Toyota Corolla  3000
  9    Mid  Honda  Accord  2500
  10   Mid Nissan  Xtrail  2200
  11   Mid Toyota   Camry  1800
  12   Mid Nissan    Moon   800
  </code></pre>
  
  <p>When I plot the sales by model using <code>ggplot</code> as below I get the plot in the picture</p>
  
  <pre><code>ggplot(df, aes(x=model, y=sales)) +
    geom_point()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mjcBM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mjcBM.jpg" alt="enter image description here"></a></p>
  
  <p>As expected the x-axis labels for <code>model</code> are in the ascending order as per their levels - <code>Accord</code> comes first and <code>Yeti</code> the last.</p>
  
  <pre><code>&gt; str(df)
  'data.frame':   12 obs. of  4 variables:
   $ tier : Factor w/ 2 levels "entry","Mid": 1 1 1 1 1 1 2 2 2 2 ...
   $ make : Factor w/ 3 levels "Honda","Nissan",..: 3 1 2 1 3 2 1 3 1 2 ...
   $ model: Factor w/ 12 levels "Accord","Amaze",..: 12 7 10 2 8 3 5 6 1 11 ...
   $ sales: int  10000 8000 5000 4000 3500 2000 4000 3000 2500 2200 ...
  &gt;
  </code></pre>
  
  <p>However, I need the plot with a different order of <code>model</code> - which is obtained when the table is ordered by tier, make and sales(descending). I can get this ordering for the table as in the code below - how do I get the same order of x-axis labels for <code>model</code> in the plot ? </p>
  
  <pre><code>&gt; df[with(df, order(tier, make, -sales)),]
      tier   make   model sales
  2  entry  Honda    Jazz  8000
  4  entry  Honda   Amaze  4000
  3  entry Nissan   Sunny  5000
  6  entry Nissan    Beat  2000
  1  entry Toyota    Yeti 10000
  5  entry Toyota Model10  3500
  7    Mid  Honda   Civic  4000
  9    Mid  Honda  Accord  2500
  10   Mid Nissan  Xtrail  2200
  12   Mid Nissan    Moon   800
  8    Mid Toyota Corolla  3000
  11   Mid Toyota   Camry  1800
  &gt; 
  </code></pre>