Question: I'm trying to plot a geom\_bar with the stack order depending on the cumulative value of each group in each bar. therefore, each bar should have a different order. Here is a reproducible example

```
set.seed(8)

#create dataframe
name <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z", "aa")
group <- c("1","1","1","2","2", "2", "1","1","1","1","1","1","1","1","1","1","1","1","2","2","2","2","2","2","2","2")
subgroup <- c("C", "C", "C", "C", "C", "C", "A", "A", "A","A","A","A","B","B","B","B","B","B","B","B", "C", "C", "C", "C", "C", "C")
value <- runif(26, min = 0, max = 10)

df <- data.frame(name, group, subgroup,value)

#plot
ggplot(data=df, aes(x=group, y=value)) + 
  geom_bar(stat="identity",aes(fill=reorder(subgroup, value)))

```

The latter results in the following plot:
[![enter image description here](https://i.stack.imgur.com/MlXRV.png)](https://i.stack.imgur.com/MlXRV.png)

Note that it is clear that the B subgroup represents a higher value than C in group 1, but it is plotted above C. I want that, for each group, the subgroups are ordered based on their cumulative value in the group. Does anybody know how to achieve this? Thanks in advance!

Answer: 
You could do this by precalculating the counts and using `geom_tile` instead of `geom_bar`. This way will work automatically with any number of groups and subgroups without manual intervention.

```
library(dplyr); library(ggplot2)
df %>%
  count(group, subgroup, wt = value) %>%
  group_by(group) %>%
  arrange(group, -n) %>%
  mutate(middle = cumsum(n) - n/2) %>% 
  ungroup() %>%

  ggplot(aes(group, fill = subgroup)) +
  geom_tile(aes(y = middle, height = n), width = 0.9)

```

[![enter image description here](https://i.stack.imgur.com/Moml6.png)](https://i.stack.imgur.com/Moml6.png)
Try using a new column with the cumulative values and then use fct\_reorder to order the stacks.

see example below:

```
df %>%
   group_by(group,subgroup) %>% 
   mutate(csum = cumsum(value)) %>% 
   ggplot()+
   aes(x = group, y = csum)+
   geom_bar(stat = "identity", aes(fill = fct_reorder(subgroup, csum)))

```

[enter image description here](https://i.stack.imgur.com/jWrzz.png)