how to reorder a flipped stacked bar in ggplot?

30 Views Asked by At

Consider the following example:

dd <- tibble(group = c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'),
             type = c('auto', 'bike', 'walk','auto', 'bike', 'walk','auto', 'bike', 'walk'),
             value = c(12,32,13,60,46,34,24,3,55))

I am trying to get control over the way the bars are shown in a stacked bar chart. The simple ggplot call is

dd %>% 
ggplot(aes(y = group, fill = type, x = value)) + geom_col()

enter image description here

However, this default rendering does not work. I would like to sort the bar chart according to the total size of the bar, in descending order. That is, group b should be on top, then a, then c. I was unable to do so using forcats. Any ideas?

Thanks!

0

There are 0 best solutions below