title: Reorder x axis in bar chart in ggplot
body: <p>I want to plot a bar chart and order the x axis by value. I know that I managed to do that before, but I might have deleted that code somewhere. I have been stuck on that now for a while, so I'm asking you now.</p>
  <p>As my dataframe contains more than 24,000 rows, here a sample data. I hope that it is enough to recreate it.</p>
  <pre><code>structure(list(country = c(&quot;Palestine&quot;, &quot;Iraq&quot;, &quot;Algeria&quot;, &quot;Libya&quot;, 
  &quot;Lebanon&quot;, &quot;Jordan&quot;, &quot;Egypt&quot;, &quot;Palestine&quot;, &quot;Libya&quot;, &quot;Egypt&quot;, 
  &quot;Libya&quot;, &quot;Jordan&quot;, &quot;Yemen&quot;, &quot;Jordan&quot;, &quot;Tunesia&quot;, &quot;Iraq&quot;, &quot;Lebanon&quot;, 
  &quot;Lebanon&quot;, &quot;Morocco&quot;, &quot;Algeria&quot;, &quot;Jordan&quot;, &quot;Egypt&quot;, &quot;Kuwait&quot;, 
  &quot;Morocco&quot;, &quot;Tunesia&quot;, &quot;Palestine&quot;, &quot;Yemen&quot;, &quot;Lebanon&quot;, &quot;Sudan&quot;, 
  &quot;Lebanon&quot;, &quot;Libya&quot;, &quot;Palestine&quot;, &quot;Yemen&quot;, &quot;Tunesia&quot;, &quot;Sudan&quot;, 
  &quot;Yemen&quot;, &quot;Morocco&quot;, &quot;Jordan&quot;, &quot;Palestine&quot;, &quot;Palestine&quot;, &quot;Libya&quot;, 
  &quot;Palestine&quot;, &quot;Libya&quot;, &quot;Jordan&quot;, &quot;Jordan&quot;, &quot;Lebanon&quot;, &quot;Iraq&quot;, 
  &quot;Algeria&quot;, &quot;Yemen&quot;, &quot;Tunesia&quot;, &quot;Lebanon&quot;, &quot;Libya&quot;, &quot;Yemen&quot;, &quot;Egypt&quot;, 
  &quot;Yemen&quot;, &quot;Libya&quot;, &quot;Palestine&quot;, &quot;Egypt&quot;, &quot;Tunesia&quot;, &quot;Sudan&quot;, &quot;Tunesia&quot;, 
  &quot;Egypt&quot;, &quot;Lebanon&quot;, &quot;Iraq&quot;, &quot;Kuwait&quot;, &quot;Libya&quot;, &quot;Tunesia&quot;, &quot;Algeria&quot;, 
  &quot;Morocco&quot;, &quot;Egypt&quot;, &quot;Tunesia&quot;, &quot;Morocco&quot;, &quot;Palestine&quot;, &quot;Kuwait&quot;, 
  &quot;Morocco&quot;, &quot;Kuwait&quot;, &quot;Morocco&quot;, &quot;Palestine&quot;, &quot;Morocco&quot;, &quot;Lebanon&quot;, 
  &quot;Iraq&quot;, &quot;Egypt&quot;, &quot;Morocco&quot;, &quot;Algeria&quot;, &quot;Jordan&quot;, &quot;Sudan&quot;, &quot;Sudan&quot;, 
  &quot;Algeria&quot;, &quot;Sudan&quot;, &quot;Egypt&quot;, &quot;Palestine&quot;, &quot;Jordan&quot;, &quot;Sudan&quot;, 
  &quot;Iraq&quot;, &quot;Egypt&quot;, &quot;Tunesia&quot;, &quot;Sudan&quot;, &quot;Yemen&quot;, &quot;Lebanon&quot;, &quot;Iraq&quot;
  ), female_head_gov = structure(c(3L, 3L, 4L, 3L, 2L, 2L, 4L, 
  2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 3L, 
  3L, 4L, 2L, 4L, 2L, 1L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 5L, 2L, 2L, 
  4L, 2L, 4L, 2L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 2L, 2L, 2L, 1L, 3L, 
  2L, 2L, 2L, 2L, 3L, 5L, 1L, 1L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 2L, 
  4L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 4L, 1L, 2L, 
  3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L), .Label = c(&quot;I strongly agree&quot;, 
  &quot;I agree&quot;, &quot;I disagree&quot;, &quot;I strongly disagree&quot;, &quot;Don't know&quot;), class = &quot;factor&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -100L))
  </code></pre>
  <p>I tried the following code using forcats::fct_reorder, but it doesn't give me the desired outcome:</p>
  <pre><code>dataset %&gt;%
    group_by(country) %&gt;%
    filter(!is.na(female_head_gov), female_head_gov != &quot;Don't know&quot;) %&gt;%
    mutate(female_head_gov = fct_collapse(female_head_gov,
                                  `Agree/strongly agree` = c(&quot;I strongly agree&quot;, &quot;I agree&quot;),
                                  `Disagree/strongly disagree` = c(&quot;I disagree&quot;, &quot;I strongly disagree&quot;))
    ) %&gt;%
    count(female_head_gov) %&gt;%
    mutate(prop = n / sum(n)) %&gt;%
    ggplot(aes(fct_reorder(country, prop), prop, fill = female_head_gov)) +
    geom_col(position = &quot;fill&quot;)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/dmPwA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dmPwA.png" alt="enter image description here" /></a></p>
  <p>I also tried this without calculating the proportions, but then I don't have variable to order the country variable with.</p>
  <pre><code>dataset %&gt;%
    group_by(country) %&gt;%
    filter(!is.na(female_head_gov), female_head_gov != &quot;Don't know&quot;) %&gt;%
    mutate(female_head_gov = fct_collapse(female_head_gov,
                                          `Agree/strongly agree` = c(&quot;I strongly agree&quot;, &quot;I agree&quot;),
                                          `Disagree/strongly disagree` = c(&quot;I disagree&quot;, &quot;I strongly disagree&quot;))
    ) %&gt;%
    ggplot(aes(country, fill = female_head_gov)) +
    geom_bar(position = &quot;fill&quot;)
  </code></pre>
  <p>Any ideas what I'm doing wrong? If you have any tips how to create such a bar chart more efficient, just let me know. :)</p>