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
443 stars 45 forks source link

Simpler syntax for data+mapping #212

Open knuesel opened 3 years ago

knuesel commented 3 years ago

This is to discuss a simpler syntax for passing data and mappings together, as requested on Discourse.

The given example was

x = range(-π, π, length=100)
y = sin.(x)

df = (; x, y)
data(df) * mapping(:x, :y) * visual(Lines)

In Gadfly the last two lines are simply layer(x = x, y = y, Geom.line).

It's actually not that bad currently as we can use the pre-grouped syntax and write

x = range(-π, π, length=100)
y = sin.(x)

mapping([x] => "x", [y] => "y") * visual(Lines) |> draw

image

or more complex:

mapping([x] => "x", [sin.(x), cos.(x)] => "y", color=["sin", "cos"]) * visual(Lines) |> draw

image

What I see missing is

  1. Support for mapping(x, y) as shorthand for mapping([x], [y])

    Maybe also support matrices. For example if A = [a1 a2] is a two-column matrix, mapping(A) could be a shorthand for mapping([[a1] [a2]]).

  2. A way to unlink axes (#203), to support unrelated subplots such as

    a = 0:0.1:3
    b = 5:0.1:7
    mapping([a, b], [sin.(a), sqrt.(b)], layout=["sin", "sqrt"]) *
    visual(Lines) |> draw
  3. Some bug(?)fixes as the following don't seem to work:

    • mapping([x], [sin.(x), cos.(x)], layout=["sin", "cos"]) works but with col it gives InexactError: trunc(Int64, NaN)
    • mapping([x], [sin.(x), cos.(x)], color=dims(1)) works but it fails with [sin.(x), cos.(x)] => "y" (gives only one color, and with .=> it produces garbage).
    • mapping([a, b] => ["a", "b"], [sin.(a), sqrt.(b)], layout=["sin", "sqrt"]) fails with MethodError: objects of type Vector{String} are not callable, and with .=> it produces garbage.
knuesel commented 3 years ago

Regarding the support for mapping(x, y) as shorthand for mapping([x], [y]), I thought it could be useful to have a list of accepted types, to see how this would fit.

Here's what I found based on the documentation (these are actually valid types that could be used in dispatch):

It seems that Array{<:Number} and Pair{<:Array{<:Number}} would fit nicely without ambiguity?

rikhuijzer commented 3 years ago

Nice tip to use mapping([x] => "x", [y] => "y") * visual(Lines). :smile: You can also use mapping([x] => :x, [y] => :y) * visual(Lines), by the way.