title: How can I change the order of the stacked bar plot in R?
body: <p>I want to change the order of the stacked bar plot in R? I am sharing the example below and the green bar will be under the blue bar, how can I can this order? See the code below.</p>
  <pre><code>d1&lt;-data.frame(Gene=c(&quot;DNA&quot;,&quot;DNA&quot;,
                         &quot;RNA&quot;,&quot;RNA&quot;,
                         &quot;XX&quot;,&quot;XX&quot;),
             Gender=c(&quot;M&quot;,&quot;F&quot;,&quot;M&quot;,&quot;F&quot;,&quot;M&quot;,&quot;F&quot;),
             p_value=c( 0.5, 0.1,
                        0.6,0.01,
                        0.07,0.02
             ))
  
  
  p&lt;-d1 %&gt;%
    ggplot(aes(x=forcats::fct_reorder(Gene,p_value), y=p_value, fill=Gender)) +
    geom_col(color=&quot;black&quot;,position=position_dodge()) + 
    coord_flip() + 
    scale_fill_manual(values=c('#6495ED','#2E8B57'))+
    labs(x=&quot;Gene&quot;, y=&quot;p-value&quot;)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/csomx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/csomx.png" alt="enter image description here" /></a></p>