How to control group sorting in boxplot?

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

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?

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)

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.

I will use Plots.jl for non-statistical plotting, and Seaborn.jl for statistical ones. :grinning:

1 Like