Skip to content

Pre-sorted data

julia
using AlgebraOfGraphics, CairoMakie, DataFrames

Sometimes we have datasets that have an inherent order we want to preserve. For example, this dataframe has countries sorted by some_value.

julia
countries = ["Algeria", "Bolivia", "China", "Denmark", "Ecuador", "France"]
group = ["2", "3", "1", "1", "3", "2"]
some_value = exp.(sin.(1:6))

df = DataFrame(; countries, group, some_value)
sort!(df, :some_value)

df

\begin{tabular}{r|ccc} & countries & group & some_value\ \hline & String & String & Float64\ \hline 1 & Ecuador & 3 & 0.383305 \ 2 & Denmark & 1 & 0.469164 \ 3 & France & 2 & 0.756226 \ 4 & China & 1 & 1.15156 \ 5 & Algeria & 2 & 2.31978 \ 6 & Bolivia & 3 & 2.48258 \ \end

When we plot this, the categorical variable countries is sorted alphabetically by default:

julia
spec = data(df) *
    mapping(:countries, :some_value, color = :group) *
    visual(BarPlot, direction = :x)
draw(spec)

We don't want this, because we have purposefully sorted the dataframe to visualize which countries have the highest value. To retain the order, we can use the presorted helper.

julia
spec = data(df) *
    mapping(:countries => presorted, :some_value, color = :group) *
    visual(BarPlot, direction = :x)
fg = draw(spec)

We can also mark multiple variables as presorted, note how the order in the color legend shifts when we do the same for group:

julia
spec = data(df) *
    mapping(:countries => presorted, :some_value, color = :group => presorted) *
    visual(BarPlot, direction = :x)
draw(spec)