MakieOrg / AlgebraOfGraphics.jl

An algebraic spin on grammar-of-graphics data visualization in Julia. Powered by the Makie.jl plotting ecosystem.
https://aog.makie.org
MIT License
436 stars 44 forks source link

"Arguments must be sorted" error #552

Closed korsbo closed 1 month ago

korsbo commented 1 month ago

Bug description

Hi,

draw throws with "Arguments must be sorted" for a strangely hard-to-pin-down combination of plot elements. This happens when plotting into multiple layouts but it seems to depend on how many plotting elements we have as well as how many subplots.

Steps to reproduce

using DataFrames
using CairoMakie
using AlgebraOfGraphics

n_subplots = 3
n_series = 10 # works fine with 2
n_x = 10
_test_df = DataFrame(rand(n_series * n_x, n_subplots), :auto)
_test_df[:, :color] = reduce(vcat, [fill(i, n_x) for i in 1:n_series])
_test_df[:, :x] = repeat(1:n_x, n_series)

plt = data(_test_df) * sum([
    mapping(:x, Symbol("x$i"), color = :color => string, layout=direct(('A':'Z')[i])) for i in 1:n_subplots
]) * visual(Lines)
draw(plt)

Error reporting

1-element ExceptionStack:
LoadError: ArgumentError: Arguments must be sorted
Stacktrace:
  [1] mergesorted(v1::Vector{String}, v2::Vector{String})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/scales.jl:491
  [2] possibly_mergesorted
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/scales.jl:510 [inlined]
  [3] mergescales(c1::AlgebraOfGraphics.CategoricalScale{Vector{String}, Nothing}, c2::AlgebraOfGraphics.CategoricalScale{Vector{String}, Nothing})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/scales.jl:366
  [4] compute_axes_grid(d::Layers, scales::AlgebraOfGraphics.Scales; axis::Dictionaries.Dictionary{Symbol, Any})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/algebra/layers.jl:255
  [5] compute_axes_grid
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/algebra/layers.jl:233 [inlined]
  [6] compute_axes_grid(fig::Figure, d::Layers, scales::AlgebraOfGraphics.Scales; axis::Dictionaries.Dictionary{Symbol, Any})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/algebra/layers.jl:205
  [7] compute_axes_grid
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/algebra/layers.jl:202 [inlined]
  [8] #346
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:20 [inlined]
  [9] update
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:10 [inlined]
 [10] plot!(fig::Figure, d::Layers, scales::AlgebraOfGraphics.Scales; axis::Dictionaries.Dictionary{Symbol, Any})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/draw.jl:20
 [11] plot!
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:16 [inlined]
 [12] (::AlgebraOfGraphics.var"#351#352"{Dictionaries.Dictionary{Symbol, Any}, Dictionaries.Dictionary{Symbol, Any}, Dictionaries.Dictionary{Symbol, Any}, Dictionaries.Dictionary{Symbol, Any}, Layers, AlgebraOfGraphics.Scales})(f::Figure)
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:64
 [13] update
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:10 [inlined]
 [14] _draw(d::Layers, scales::AlgebraOfGraphics.Scales; axis::Dictionaries.Dictionary{Symbol, Any}, figure::Dictionaries.Dictionary{Symbol, Any}, facet::Dictionaries.Dictionary{Symbol, Any}, legend::Dictionaries.Dictionary{Symbol, Any}, colorbar::Dictionaries.Dictionary{Symbol, Any})
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:63
 [15] _draw
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:60 [inlined]
 [16] draw(d::Layers, scales::AlgebraOfGraphics.Scales; axis::@NamedTuple{}, figure::@NamedTuple{}, facet::@NamedTuple{}, legend::@NamedTuple{}, colorbar::@NamedTuple{}, palette::Nothing)
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/draw.jl:60
 [17] draw
    @ ~/data/.julia/packages/AlgebraOfGraphics/vP9dh/src/draw.jl:47 [inlined]
 [18] draw(d::Layers)
    @ AlgebraOfGraphics ~/data/.julia/packages/AlgebraOfGraphics/9G4sD/src/draw.jl:50

Version info

I get this on AoG 0.8.6, CairoMakie 0.12.9, Makie 0.21.9 with Julia 1.10.4 on Linux

korsbo commented 1 month ago

I found a better way of doing this (mapping(:x, [:x1, :x2, :x3] ...)) so it's by no means urgent, but I still would not have expected the code above to throw for some values of n_series but not others.

jkrumbiegel commented 1 month ago

Thanks for the reproducer, the problem was a bug in the sorting machinery. The reason it seemed to depend on the number of elements was just that it only appeared for strings that had to be naturally sorted, so you needed the "10" in there to trigger it. This will be fixed by https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/553