title: R ggplot2 geom_bar change/flip order of legend items
body: <p>Based on the data and code below, how can I change of the legend items such that the legend item <code>Males</code> is under the <code>Male portion</code> of the <code>pyramid plot</code> and vice versa?</p>
  <p>I tried:</p>
  <pre><code># Change the order of values to reflect them in the plot
    pop_hisp_df$Type = factor(pop_hisp_df$Type, levels = rev(pop_hisp_df$Type))
  
  Error in `levels&lt;-`(`*tmp*`, value = as.character(levels)) : 
    factor level [2] is duplicated
  </code></pre>
  <p>Current Output:</p>
  <p><a href="https://i.stack.imgur.com/kWdLw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kWdLw.png" alt="enter image description here" /></a></p>
  <p>Data (<code>pop_hisp_df</code>):</p>
  <pre><code>structure(list(age_group = c(&quot;&lt;  5 years&quot;, &quot;5 - 14&quot;, &quot;15  -  24&quot;, 
  &quot;25  -  34&quot;, &quot;35  -  44&quot;, &quot;45  -  54&quot;, &quot;55  -  64&quot;, &quot;65  -  74&quot;, 
  &quot;75  -  84&quot;, &quot;85 +&quot;, &quot;&lt;  5 years&quot;, &quot;5 - 14&quot;, &quot;15  -  24&quot;, &quot;25  -  34&quot;, 
  &quot;35  -  44&quot;, &quot;45  -  54&quot;, &quot;55  -  64&quot;, &quot;65  -  74&quot;, &quot;75  -  84&quot;, 
  &quot;85 +&quot;), Type = c(&quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, 
  &quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, &quot;Males&quot;, &quot;Females&quot;, &quot;Females&quot;, 
  &quot;Females&quot;, &quot;Females&quot;, &quot;Females&quot;, &quot;Females&quot;, &quot;Females&quot;, &quot;Females&quot;, 
  &quot;Females&quot;, &quot;Females&quot;), Value = c(-6, -13, -13, -15, -17, -15, 
  -11, -6, -3, -1, 6, 12, 12, 14, 16, 15, 12, 7, 4, 2)), row.names = c(NA, 
  -20L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>Code:</p>
  <pre><code>library(tidyverse)
  library(plotly)
  
  # Plot
      gg_pop_hisp = ggplot(pop_hisp_df, aes( x = forcats::as_factor(age_group), y = Value, fill = Type)) +
        geom_bar(data = subset(pop_hisp_df, Type == &quot;Females&quot;), stat = &quot;identity&quot;) +
        geom_bar(data = subset(pop_hisp_df, Type == &quot;Males&quot;), stat = &quot;identity&quot;) +
        #geom_text(aes(label = paste0(abs(Value), &quot;%&quot;))) +
        scale_y_continuous(limits=c(-20,20),
                           breaks=c(-15,-10,0,10,15),
                           labels=paste0(c(15,10,0,10,15),&quot;%&quot;)) +          # CHANGE
        scale_fill_manual(name = &quot;&quot;, values = c(&quot;Females&quot;=&quot;#FC921F&quot;, &quot;Males&quot;=&quot;#149ECE&quot;), labels = c(&quot;Females&quot;, &quot;Males&quot;)) +
        ggtitle(&quot;HISPANIC POPULATION BY GENDER AND AGE GROUP&quot;) +
        labs(x = &quot;AGE GROUPS&quot;, y = &quot;PERCENTAGE POPULATION&quot;, fill = &quot;Gender&quot;) +
        theme_minimal() +
        theme(legend.position=&quot;bottom&quot;) +
        coord_flip()  
      
      # Interactive
      ggplotly(gg_pop_hisp) %&gt;% 
        layout(
          legend = list(
            orientation = 'h', x = 0.3, y = -0.3, 
            title = list(text = '')))
  </code></pre>