title: Order legend with stacked bar chart - ggplot
body: <p>I'm trying to create a stacked barchart, everything is working except that it's hard to interpret the chart due to the ordering of the legend in connection with the <code>fill</code> aestethic. </p>
  
  <p><a href="https://i.stack.imgur.com/irMF3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/irMF3.png" alt="enter image description here"></a></p>
  
  <p>I want the stacked bar to follow the order of the legend names, that will say first 'Accomodation', then 'Car expenses' for each household category, respectively. </p>
  
  <p>Now I made a try but this doesn't seem to work, it involves by rearranging the levels attribute of the factor column. </p>
  
  <p>My code</p>
  
  <pre><code> ggraph$names &lt;- factor(ggraph$names)
    levels(ggraph$names) &lt;- rownames(do.call(rbind, tapply(ggraph[,2], list(ggraph$names), sum, simplify = FALSE)))
  
    ggplot(ggraph, aes(x = household, y = absChange, fill = names)) + 
      geom_bar(stat = 'identity') + 
      scale_y_continuous(labels = scales::comma) +
      coord_flip()
  </code></pre>
  
  <p>ggraph object</p>
  
  <pre><code>&gt; dput(ggraph)
  structure(list(names = structure(c(1L, 6L, 4L, 8L, 3L, 1L, 4L, 
  6L, 8L, 3L, 1L, 6L, 4L, 8L, 3L, 6L, 8L, 1L, 4L, 9L, 1L, 6L, 4L, 
  5L, 2L, 6L, 1L, 8L, 4L, 3L, 1L, 6L, 7L, 4L, 3L), .Label = c("Accomodation", 
  "Car expenses", "Groceries", "Household-services", "Interests expenses", 
  "Leisure and culture", "Rent (incl garage fee)", "Transportation", 
  "Travels, hotel stays"), class = "factor"), absChange = c(18470L, 
  16030L, 11540L, 8010L, 6150L, 22740L, 17600L, 14070L, 12990L, 
  7520L, 18170L, 17280L, 14720L, 9620L, 5960L, 48340L, 31710L, 
  26730L, 16770L, 10520L, 23160L, 17980L, 14030L, 10700L, 10570L, 
  16100L, 13930L, 7660L, 7650L, 7320L, 15430L, 13500L, 5900L, 5740L, 
  4250L), household = c("all households", "all households", "all households", 
  "all households", "all households", "cohabit with child", "cohabit with child", 
  "cohabit with child", "cohabit with child", "cohabit with child", 
  "cohabit without child", "cohabit without child", "cohabit without child", 
  "cohabit without child", "cohabit without child", "other cohabit with child", 
  "other cohabit with child", "other cohabit with child", "other cohabit with child", 
  "other cohabit with child", "other households", "other households", 
  "other households", "other households", "other households", "single parent", 
  "single parent", "single parent", "single parent", "single parent", 
  "single parent without child", "single parent without child", 
  "single parent without child", "single parent without child", 
  "single parent without child")), .Names = c("names", "absChange", 
  "household"), row.names = c(2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
  11L, 12L, 14L, 15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 26L, 
  27L, 28L, 29L, 30L, 32L, 33L, 34L, 35L, 36L, 38L, 39L, 40L, 41L, 
  42L), class = "data.frame")
  </code></pre>