# How can I change the series order of the legend in Plots?

**URL:** https://discourse.julialang.org/t/how-can-i-change-the-series-order-of-the-legend-in-plots/20278
**Category:** Visualization
**Created:** [January 30, 2019, 2:43pm UTC](https://discourse.julialang.org/t/how-can-i-change-the-series-order-of-the-legend-in-plots/20278 "2019-01-30T14:43:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)
#### Post date: [January 30, 2019, 2:43pm UTC](https://discourse.julialang.org/t/how-can-i-change-the-series-order-of-the-legend-in-plots/20278/1 "2019-01-30T14:43:21Z")

</div>

When making stacked bar plots or stacked area plots, the default series order in the legend appears upside-down compared to the order in the plot itself. Example using the `groupedbar()` recipe provided by StatPlots:

```julia
using StatPlots
plotly()
groupedbar(rand(10,5), bar_position = :stack, lab=["A" "B" "C" "D" "E"])

```

![newplot%20(3)](https://global.discourse-cdn.com/julialang/original/3X/8/b/8b9c5eebb5767d4b5c81e89d64ba0beab130a002.png)

The official word is that [you can’t currently specify the order](https://github.com/JuliaPlots/StatsPlots.jl/issues/140), but can someone come up with a hackish solution? I have to make a large number of stacked plots with 10+ series each, and I really don’t want to fix all those legends manually in Photoshop.

---

<div class="post-metadata">

### Author: ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)
#### Post date: [January 30, 2019, 6:26pm UTC](https://discourse.julialang.org/t/how-can-i-change-the-series-order-of-the-legend-in-plots/20278/2 "2019-01-30T18:26:22Z")

</div>

I came up with a disgusting little hack myself, I’ll explain it here if anyone else needs something similar.

The idea is to add more bars with dummy data (all zeros) to the plot using 5 (in this case) additional color categories, reusing the same 5 colors but in reverse order, then hide the legend entries for the original data, and finally showing legend entries for the new colors using the original legend labels. Like this:

```julia
groupedbar([1:10;12;12], [rand(10,5) zeros(10,5); zeros(2,10)], bar_position = :stack,
        lab=["" "" "" "" "" "A" "B" "C" "D" "E"], color=[1 2 3 4 5 5 4 3 2 1], xlim=(0,11))

```

![newplot%20(4)](https://global.discourse-cdn.com/julialang/original/3X/4/3/4357c0835f044b01c04db9d90311920c8cf2914c.png)

Note that I added two new bars with dummy data. Strangely, when I just added one the width of the bars changed a bit. (Why?)

Like I said, a nasty hack, but it’s easy to hide away the ugliness in a function. If anyone else has a better idea then please share.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [January 30, 2019, 9:46pm UTC](https://discourse.julialang.org/t/how-can-i-change-the-series-order-of-the-legend-in-plots/20278/3 "2019-01-30T21:46:50Z")

</div>

This is a bug in groupedbar, though. The colors of the bars should be reversed. You could take a look at just fixing the recipe?
