Stacked barchart, independent fill order for each stack

If you use separate geom_bars, you can make the orders different.

dats %>% 
  ggplot(aes(x = id, y = value, fill = reorder(filling,-ordering))) + 
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 1)) +
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 2)) +
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 3)) +
    guides(fill=guide_legend("ordering")) 

enter image description here

More generally:

bars <- map(unique(dats$id)
            , ~geom_bar(stat = "identity", position = "stack"
                       , data = dats %>% filter(id == .x)))

dats %>% 
  ggplot(aes(x = id, y = value, fill = reorder(filling,-ordering))) + 
    bars +
    guides(fill=guide_legend("ordering"))

Leave a Comment