How to order axis with categorical variable in AlgebraOfGraphics?

Hello! I’m experimenting with AlgebraOfGraphics and stumbled upon something I haven’t been able to do. I looks like the default behaviour when plotting categorical variable on the y axis and continuous on the x axis is to order the y axis in ascending lexical order. Instead, I would like to order the y axis in the ascending order of values on the x axis. Is there a way to do this?

using CairoMakie, AlgebraOfGraphics, ColorSchemes
let
    df = DataFrame(x = [0.15, 0.13, 0.17], c = ["A", "B", "C"])
    plt = data(df) * mapping(:x, :c, color=:c) * visual(markersize=40)
    draw(
        plt;
        axis=(;limits=(0.1, 0.2, nothing, nothing),
        xticks=collect(0.1:0.02:0.2))
    )
end

The goal is for the y axis to be ordered (ascending) B, A, C.

By default a categorical variable is sorted according to its own values, but you can specify an alternative order with mapping(:c => sorter(...)) :

using CairoMakie, AlgebraOfGraphics, DataFrames

df = DataFrame(x = [0.15, 0.13, 0.17], c = ["A", "B", "C"])

c_order = sort(df, :x).c

plt = data(df) *
      mapping(:x, :c => sorter(c_order), color=:c) *
      visual(markersize=40)

draw(plt; axis=(; limits=((0.1, 0.2), nothing), xticks=0.1:0.02:0.2))

1 Like