title: Part 2: How to correctly order segments by value, within an individual bar, on a bar chart in ggplot
body: <p>So I thought I had my question answered with <a href="https://stackoverflow.com/questions/61813491/how-to-correctly-order-segments-by-value-within-an-individual-bar-on-a-bar-cha">my prior question</a>, but alas something is still not working.  I am wondering if there is something in the structure of my data that I am missing because my prior question with fake data worked.</p>
  
  <p>Here is a new reproducible example, with dummy data that more closely replicates my data set and my problem.  My question is how do I sort the segments <strong><em>within each bar</em></strong> of the bar chart by value (largest value within a bar on the bottom, smallest on top).  </p>
  
  <pre><code>library(dplyr)
  
  repro_df &lt;- structure(list(Grp = structure(c(5L, 7L, 2L, 3L, 8L, 7L, 10L, 
                                               4L, 4L, 3L, 2L, 2L, 3L, 8L, 9L, 3L, 3L, 6L, 6L, 5L, 6L, 8L, 4L, 
                                               11L, 5L, 1L, 10L, 8L, 1L, 6L, 3L, 1L, 1L, 9L, 5L, 3L, 5L, 4L, 
                                               5L, 5L, 2L, 1L, 9L, 4L, 5L, 10L, 6L, 8L, 3L, 6L, 2L, 6L, 4L, 
                                               7L, 2L, 8L, 9L, 9L, 10L, 5L, 1L, 9L, 1L, 5L, 2L, 8L, 8L, 3L, 
                                               3L, 10L, 7L, 6L, 9L, 2L, 9L, 7L, 1L, 1L, 9L, 1L, 11L, 10L, 9L, 
                                               3L, 7L, 2L, 4L, 7L, 6L, 6L, 4L, 8L, 5L, 5L, 7L, 10L, 8L, 3L, 
                                               6L, 3L, 10L, 10L, 7L, 8L, 9L, 8L, 5L, 7L, 3L, 10L, 11L, 7L, 4L, 
                                               10L, 3L, 8L, 5L, 3L, 5L, 4L, 3L, 10L, 7L, 3L, 4L, 9L, 2L, 3L, 
                                               2L, 1L, 8L, 11L, 2L, 1L, 7L), .Label = c("0", "1", "2", "3", 
                                                                                        "4", "5", "6", "7", "8", "9", "10"), class = "factor"), Segment = structure(c(1L, 
                                                                                                                                                                      2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                                                                                                                                                      3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
                                                                                                                                                                      1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                                                                                                                                                      2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                                                                                                                                                      3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
                                                                                                                                                                      1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                                                                                                                                                      2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
                                                                                                                                                                      3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
                                                                                                                                                                      1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
                             Value = c(914, NA, NA, 228, NA, NA, NA, 207, NA, 179, NA, 
                                       NA, 149, NA, NA, 135, NA, NA, NA, 109, NA, NA, 105, NA, NA, 
                                       101, NA, 100, NA, NA, NA, 98, NA, 96, NA, NA, 87, NA, NA, 
                                       77, NA, NA, 74, NA, NA, 57, NA, NA, 49, NA, NA, 35, NA, NA, 
                                       31, NA, NA, 25, NA, NA, NA, 25, NA, NA, 21, NA, 18, NA, NA, 
                                       16, NA, NA, 8, NA, NA, 7, NA, NA, 7, NA, NA, 5, NA, NA, NA, 
                                       NA, 4, NA, NA, 0, 0, NA, NA, 0, NA, NA, 0, NA, NA, NA, NA, 
                                       0, 0, NA, NA, NA, NA, 0, 0, NA, NA, 0, NA, NA, NA, NA, 0, 
                                       NA, NA, 0, NA, NA, 0, 0, NA, NA, NA, 0, NA, NA, NA, 0, NA, 
                                       NA, 0)), class = "data.frame", row.names = c(NA, -135L))
  
  # Reorder the data frame
  repro_order_df &lt;- repro_df %&gt;% 
    group_by(Segment) %&gt;% 
    mutate(Grp = fct_reorder(Grp, Value))
  
  head(repro_order_df, 10)
  # A tibble: 10 x 3
  # Groups:   Segment [3]
     Grp   Segment Value
     &lt;fct&gt; &lt;fct&gt;   &lt;dbl&gt;
   1 4     A         914
   2 6     B          NA
   3 1     C          NA
   4 2     A         228
   5 7     B          NA
   6 6     C          NA
   7 9     A          NA
   8 3     B         207
   9 3     C          NA
  10 2     A         179
  
  # Plot
  ggplot(repro_order_df, aes(x=Segment, y=Value, fill=Grp)) +
    geom_col(color = "black")
  </code></pre>
  
  <p>When I graph this data after reordering, each bar is not ordered by Value as I would have expected.  A bit more oddly, in my real data set the first bar is ordered correctly but the following bars are not.  Any thoughts as to why this is not working?
  <a href="https://i.stack.imgur.com/J1TUo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J1TUo.png" alt="plot"></a></p>
  
  <p>Thanks!</p>