title: R: ggplot2 grouped bar plot is not in order
body: <p>I've encountered the problem that my <code>ggplot2</code> grouped bar chart is not in the order implied by my dataframe (i. e. Other, Medical &amp; Health, IT, Industrial and respectively MM Levered, Leverage Effect, ..., Combination R/M Effect).</p>
  <p>In the following I will provide you with my current ggplot2-Code:</p>
  <pre><code>#Grouped bar chart with ggplot2
  ggplot(GraphTableIndustry,
         aes(x = ValueEffects,
             y = value, #column name of groups
             fill = variable)) + #column name of values
    geom_bar(stat = &quot;identity&quot;,
             position = &quot;dodge&quot;) +
    geom_text(aes(label = value),
              vjust = -0.5,
              color = &quot;black&quot;,
              position = position_dodge(width = 0.9),
              size = 3.5) +
      theme(axis.line = element_blank(),
          axis.text.y = element_blank(),
          axis.text = element_text(
            size = 10,
            color = &quot;black&quot;),
          axis.ticks = element_blank(),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          panel.background = element_blank(),
          panel.border = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          plot.background = element_blank(),
          legend.text = element_text(
            size = 10,
            color = &quot;black&quot;),
          legend.title = element_blank()
          ) +
    theme(legend.position = &quot;bottom&quot;) +
    scale_fill_brewer(palette = &quot;Blues&quot;, direction = -1)
  </code></pre>
  <p>While researching I came up with the following code (exemplarily for the subgroups):</p>
  <pre><code>GraphTableIndustry$variable &lt;- factor(GraphTableIndustry$variable, levels = c('Other', 'Medical &amp; Health', 'IT', 'Industrial'))
  </code></pre>
  <p>However, even though the code runs, it does not seem to have any impact on the plot. Interestingly enough, the plot seems to be sorted by values within subgroups, but not in groups.
  Any help would be greatly appreciated as I am lost as to why the order is all messed up. Thanks in advance.</p>
  <p><strong>Current bar plot:</strong>
  <a href="https://i.stack.imgur.com/D9fhY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D9fhY.jpg" alt="Current bar plot" /></a></p>
  <p><strong>Reproducible sample:</strong></p>
  <pre><code>structure(list(ValueEffects = c(&quot;MM Levered&quot;, &quot;Leverage Effect&quot;, 
  &quot;MM Unlevered&quot;, &quot;Multiple Effect&quot;, &quot;Combination Effect&quot;, &quot;Operating Improvements&quot;, 
  &quot;FCF Effect&quot;, &quot;EBITDA Effect&quot;, &quot;Revenue Effect&quot;, &quot;Margin Effect&quot;, 
  &quot;Combination R/M Effect&quot;, &quot;MM Levered&quot;, &quot;Leverage Effect&quot;, &quot;MM Unlevered&quot;, 
  &quot;Multiple Effect&quot;, &quot;Combination Effect&quot;, &quot;Operating Improvements&quot;, 
  &quot;FCF Effect&quot;, &quot;EBITDA Effect&quot;, &quot;Revenue Effect&quot;, &quot;Margin Effect&quot;, 
  &quot;Combination R/M Effect&quot;, &quot;MM Levered&quot;, &quot;Leverage Effect&quot;, &quot;MM Unlevered&quot;, 
  &quot;Multiple Effect&quot;, &quot;Combination Effect&quot;, &quot;Operating Improvements&quot;, 
  &quot;FCF Effect&quot;, &quot;EBITDA Effect&quot;, &quot;Revenue Effect&quot;, &quot;Margin Effect&quot;, 
  &quot;Combination R/M Effect&quot;, &quot;MM Levered&quot;, &quot;Leverage Effect&quot;, &quot;MM Unlevered&quot;, 
  &quot;Multiple Effect&quot;, &quot;Combination Effect&quot;, &quot;Operating Improvements&quot;, 
  &quot;FCF Effect&quot;, &quot;EBITDA Effect&quot;, &quot;Revenue Effect&quot;, &quot;Margin Effect&quot;, 
  &quot;Combination R/M Effect&quot;), variable = structure(c(1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c(&quot;Other&quot;, &quot;Medical &amp; Health&quot;, 
  &quot;IT&quot;, &quot;Industrial&quot;), class = &quot;factor&quot;), value = c(&quot; 3.0&quot;, &quot; 0.2&quot;, 
  &quot; 2.8&quot;, &quot; 1.6&quot;, &quot; 6.1&quot;, &quot;10.5&quot;, &quot; 0.0&quot;, &quot;10.5&quot;, &quot;12.6&quot;, &quot; 2.0&quot;, 
  &quot; 4.1&quot;, &quot; 3.0&quot;, &quot; 0.2&quot;, &quot; 2.8&quot;, &quot; 1.6&quot;, &quot; 6.8&quot;, &quot;11.2&quot;, &quot; 0.0&quot;, 
  &quot;11.2&quot;, &quot;37.5&quot;, &quot; 0.5&quot;, &quot;26.8&quot;, &quot; 2.8&quot;, &quot; 0.1&quot;, &quot; 2.9&quot;, &quot; 2.6&quot;, 
  &quot; 3.0&quot;, &quot; 8.4&quot;, &quot; 0.2&quot;, &quot; 8.6&quot;, &quot; 6.5&quot;, &quot; 3.3&quot;, &quot; 1.1&quot;, &quot; 2.7&quot;, 
  &quot; 0.2&quot;, &quot; 2.5&quot;, &quot; 1.5&quot;, &quot; 3.3&quot;, &quot; 7.4&quot;, &quot; 0.1&quot;, &quot; 7.4&quot;, &quot;20.1&quot;, 
  &quot; 0.6&quot;, &quot;13.2&quot;)), row.names = c(NA, -44L), class = &quot;data.frame&quot;)
  </code></pre>