title: Changing the order of elements in stacked barchart by filter with ggplot2
body: <p>I would need some help with a Likert-scala bar chart that I created using ggplot2. Here is the data frame:</p>
  <pre class="lang-r prettyprint-override"><code>structure(list(Q4_ROLE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
  3L), levels = c(&quot;Civilian Analyst&quot;, &quot;Military Analyst&quot;, &quot;Operations/Admin Specialist&quot;
  ), class = &quot;factor&quot;), Year = structure(c(1L, 1L, 1L, 1L, 1L, 
  2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
  1L, 2L), levels = c(&quot;2021&quot;, &quot;2022&quot;), class = &quot;factor&quot;), Q20_A8 = structure(c(1L, 
  2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
  4L, 5L, 3L, 4L, 5L, 3L), levels = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;), class = &quot;factor&quot;), 
      n = c(1L, 4L, 12L, 25L, 17L, 7L, 16L, 16L, 16L, 7L, 1L, 2L, 
      4L, 8L, 5L, 8L, 1L, 2L, 1L, 3L, 2L, 1L, 3L), perc = c(1.69491525423729, 
      6.77966101694915, 20.3389830508475, 42.3728813559322, 28.8135593220339, 
      11.2903225806452, 25.8064516129032, 25.8064516129032, 25.8064516129032, 
      11.2903225806452, 6.66666666666667, 13.3333333333333, 26.6666666666667, 
      53.3333333333333, 29.4117647058824, 47.0588235294118, 5.88235294117647, 
      11.7647058823529, 5.88235294117647, 50, 33.3333333333333, 
      16.6666666666667, 100), percent_answers = c(-0.0169491525423729, 
      -0.0677966101694915, 0.203389830508475, 0.423728813559322, 
      0.288135593220339, -0.112903225806452, -0.258064516129032, 
      0.258064516129032, 0.258064516129032, 0.112903225806452, 
      -0.0666666666666667, 0.133333333333333, 0.266666666666667, 
      0.533333333333333, -0.294117647058824, -0.470588235294118, 
      0.0588235294117647, 0.117647058823529, 0.0588235294117647, 
      0.5, 0.333333333333333, 0.166666666666667, 1), percent_answers_label = c(&quot;-2%&quot;, 
      &quot;-7%&quot;, &quot;20%&quot;, &quot;42%&quot;, &quot;29%&quot;, &quot;-11%&quot;, &quot;-26%&quot;, &quot;26%&quot;, &quot;26%&quot;, 
      &quot;11%&quot;, &quot;-7%&quot;, &quot;13%&quot;, &quot;27%&quot;, &quot;53%&quot;, &quot;-29%&quot;, &quot;-47%&quot;, &quot;6%&quot;, 
      &quot;12%&quot;, &quot;6%&quot;, &quot;50%&quot;, &quot;33%&quot;, &quot;17%&quot;, &quot;100%&quot;)), row.names = c(NA, 
  -23L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p><sup>Created on 2022-08-28 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>
  <p>I have five levels and I want them to be ordered correctly, but since I have it divergent I would need two different orderings. Using:</p>
  <p>position_stack(reverse = TRUE)</p>
  <p>works just fine when the plot was not divergent. I basically need the Neutral-Agree-Strong Agree to be reverse = TRUE and Strong disagree-Disagree to be reverse = FALSE so everything is in the right order on the divergent scale.</p>
  <p>I have tried to filter with geom_col() to make 3-5 in a different direction than 1-2 but the second command overwrites my first one, making the filtering useless.</p>
  <p>Q20_A8 is the Answer variable:</p>
  <pre><code>Factor w/ 5 levels &quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;
  </code></pre>
  <pre><code>count_8 %&gt;%
    ggplot(aes(x = Year, y = percent_answers, fill = Q20_A8)) +
            geom_col(count_8 = filter(count_8, Q20_A8 %in% c(&quot;3&quot;,&quot;4&quot;,&quot;5&quot;)), position = position_stack(reverse = TRUE )) +
            geom_col(count_8 = filter(count_8, Q20_A8 %in% c(&quot;1&quot;,&quot;2&quot;)), aes( y = percent_answers), position = position_stack(reverse = FALSE )) +
    geom_text(aes(label = percent_answers_label), size = 2.4,
              position = position_stack(reverse = FALSE, vjust = 0.5),
              color = &quot;black&quot;,
              fontface = &quot;bold&quot;) +
    facet_wrap(~ Q4_ROLE, nrow=3) +
    coord_flip() +
    theme_minimal() +
    theme(legend.title = element_text(size=8),
          legend.key.size = unit(0.3, 'cm'),
          legend.text = element_text(size = 6),
          axis.title.y = element_text(vjust = +3),
          legend.position=&quot;bottom&quot;) +
      scale_fill_manual(name=&quot;Response:&quot;,
        values=c(&quot;#C0392B&quot;,&quot;#F5B7B1&quot;,&quot;#E5E7E9&quot;, &quot;#85C1E9&quot;, &quot;#2874A6&quot;),
    labels=c(&quot;Strongly Disagree&quot;, &quot;Disagree&quot;, &quot;Neither Agree/Disagree&quot;, &quot;Agree&quot;, &quot;Strongly Agree&quot;)) +
    xlab(&quot;&quot;) +
    ylab(&quot;&quot;) +
    ggtitle(&quot;Test&quot;) +
    scale_y_continuous(limits = c(-0.5,1), labels = ylabs)
  </code></pre>
  <p>Any help is appreciated! Thank you.</p>
  <p><img src="https://i.stack.imgur.com/cjC3w.png" alt="Likert chart" /></p>