Reputation: 1529
If I plot a boxplot using StatsPlots.jl with categorical data on the x axis, StatsPlots.jl orders the axis alphabetically. How can I stop this from happening?
using DataFrames
using StatsPlots
plotly()
df = DataFrame(grade=[rand(4:0.5:6, 40)...,
rand(5:0.5:8, 20)...,
rand(8:0.5:10, 10)...],
experience=[fill("beginner", 40)...,
fill("advanced", 20)...,
fill("expert", 10)...])
boxplot(df.experience, df.grade, label=nothing)
Upvotes: 1
Views: 319
Reputation: 130
One can use a left-right space-padding trick:
xorder = ["beginner" "expert" "advanced"]
Nx = length(xorder)
str = fill("",length(df.experience))
for (i,xi) in enumerate(xorder)
j = findall(x->x==xi, df.experience)
si = " "^(Nx-i)
@. str[j] = si * string(df.experience[j]) * si
end
# solution by left-right space-padding strings
@df df boxplot(str, :grade, fillalpha=0.75, linewidth=1, label=nothing)
PS: Input uses random, so graphic changes every time.
Upvotes: 1