# How to control group sorting in boxplot?

**URL:** <https://discourse.julialang.org/t/how-to-control-group-sorting-in-boxplot/67432>\
**Category:** General Usage\
**Tags:** plotting, statsplots\
**Created:** [August 31, 2021, 3:13pm UTC](https://discourse.julialang.org/t/how-to-control-group-sorting-in-boxplot/67432 "2021-08-31T15:13:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [August 31, 2021, 3:13pm UTC](https://discourse.julialang.org/t/how-to-control-group-sorting-in-boxplot/67432/1 "2021-08-31T15:13:00Z")

</div>

How can I control the order in which groups are sorted along the x-axis? The `xsort` attribute seems to be deprecated, cf. [How to adjust the X-axis order of boxplot with StatsPlots.jl - #9 by Shuhua](https://discourse.julialang.org/t/how-to-adjust-the-x-axis-order-of-boxplot-with-statsplots-jl/56065/9)

I’m using Julia v1.6 and the `boxplot` function ignores the order of elements in the id column and sorts alphabetically. I would love to find a way to sort groups along the x-axis according to a custom id vector.

I guess I should replace the id vector elements by a number sequence and then replace x-axis tick labels. But is there no elegant solution?

---

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [August 31, 2021, 3:56pm UTC](https://discourse.julialang.org/t/how-to-control-group-sorting-in-boxplot/67432/2 "2021-08-31T15:56:29Z")

</div>

OK. I had to go with the ugly approach and replace the elements of the id column by factors of two, ie `new_tickers = 2:2:length(unique(df.idcol))*2`, in order to avoid boxplots being too close to each other. And add `xticks = (old_tickers, new_tickers)` to plot attributes. To also change the order in the legend, I added the plot attribute `labels = permutedims(new_tickers)`

PS: `old_tickers = unique(df.idcol)`

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [August 31, 2021, 4:38pm UTC](https://discourse.julialang.org/t/how-to-control-group-sorting-in-boxplot/67432/3 "2021-08-31T16:38:10Z")

</div>

Well, I feel that StatsPlots.jl is a little awkward to use. If you have experience with PyPlot in Python, then my current suggestion is to use the excellent seaborn package for **statistical plotting**. Regarding your specific requirement, there is a `order` parameter in [`seaborn.boxplot`](https://seaborn.pydata.org/generated/seaborn.boxplot.html).

I will use Plots.jl for non-statistical plotting, and [Seaborn.jl](https://github.com/JuliaPy/Seaborn.jl) for statistical ones. 😀
