Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

[WIP do not merge] algebra of graphics #112

Closed
wants to merge 4 commits into from
Closed

[WIP do not merge] algebra of graphics #112

wants to merge 4 commits into from

Conversation

piever
Copy link
Member

@piever piever commented Mar 14, 2020

No description provided.

@piever piever changed the title algebra of graphics [WIP do not merge] algebra of graphics Mar 14, 2020
@piever
Copy link
Member Author

piever commented Mar 14, 2020

Issue with MultiplePlot:

julia> using RDatasets: dataset

julia> using StatsMakie: linear

julia> using AbstractPlotting, GLMakie

julia> using AlgebraOfGraphics: Data, Select, Group

julia> mpg = dataset("ggplot2", "mpg");

julia> spec = Data(mpg) * Select(:Displ, :Hwy) * Group(color = :Year);

julia> plot(Scatter * spec, markersize=10px) # works;

julia> plot(linear * spec, markersize=10px) # works

julia> plot((Scatter + linear) * spec)
Error showing value of type Scene:
ERROR: MethodError: no method matching length(::AbstractPlotting.Automatic
)
Closest candidates are:
  length(::Core.SimpleVector) at essentials.jl:596
  length(::Base.MethodList) at reflection.jl:852
  length(::Core.MethodTable) at reflection.jl:938
  ...
Stacktrace:
 [1] _similar_for(::UnitRange{Int64}, ::Type{Any}, ::AbstractPlotting.Auto
matic, ::Base.HasLength) at ./array.jl:576
 [2] _collect(::UnitRange{Int64}, ::AbstractPlotting.Automatic, ::Base.Has
Eltype, ::Base.HasLength) at ./array.jl:609
 [3] collect(::AbstractPlotting.Automatic) at ./array.jl:603
 [4] broadcastable(::AbstractPlotting.Automatic) at ./broadcast.jl:665
 [5] broadcasted at ./broadcast.jl:1230 [inlined]
 [6] (::GLMakie.var"#63#69")(::AbstractPlotting.Automatic) at /home/pietro
/.julia/packages/GLMakie/gMxEB/src/drawing_primitives.jl:54
 [7] lift(::Function, ::Observables.Observable{AbstractPlotting.Automatic}
) at /home/pietro/.julia/packages/AbstractPlotting/AO27s/src/interaction/n
odes.jl:9
 [8] (::GLMakie.var"#58#64"{GLMakie.var"#86#87"{LineSegments{...}},GLMakie
.Screen,Scene,LineSegments{...}})() at /home/pietro/.julia/packages/GLMaki
e/gMxEB/src/drawing_primitives.jl:54
 [9] get!(::GLMakie.var"#58#64"{GLMakie.var"#86#87"{LineSegments{...}},GLM
akie.Screen,Scene,LineSegments{...}}, ::Dict{UInt64,GLMakie.GLAbstraction.
RenderObject}, ::UInt64) at ./dict.jl:452
 [10] cached_robj!(::GLMakie.var"#86#87"{LineSegments{...}}, ::GLMakie.Scr
een, ::Scene, ::LineSegments{...}) at /home/pietro/.julia/packages/GLMakie
/gMxEB/src/drawing_primitives.jl:40
 [11] draw_atomic at /home/pietro/.julia/packages/GLMakie/gMxEB/src/drawin
g_primitives.jl:178 [inlined]
 [12] insert! at /home/pietro/.julia/packages/GLMakie/gMxEB/src/drawing_pr
imitives.jl:126 [inlined]
 [13] (::GLMakie.var"#76#77"{GLMakie.Screen,Scene})(::LineSegments{...}) a
t /home/pietro/.julia/packages/GLMakie/gMxEB/src/drawing_primitives.jl:131
 [14] foreach(::GLMakie.var"#76#77"{GLMakie.Screen,Scene}, ::Array{Abstrac
tPlot,1}) at ./abstractarray.jl:1919
 [15] insert!(::GLMakie.Screen, ::Scene, ::MultiplePlot{...}) at /home/pie
tro/.julia/packages/GLMakie/gMxEB/src/drawing_primitives.jl:128
 [16] insertplots!(::GLMakie.Screen, ::Scene) at /home/pietro/.julia/packa
ges/GLMakie/gMxEB/src/screen.jl:59
 [17] backend_display(::GLMakie.Screen, ::Scene) at /home/pietro/.julia/pa
ckages/GLMakie/gMxEB/src/screen.jl:108
 [18] backend_display at /home/pietro/.julia/packages/GLMakie/gMxEB/src/gl
_backend.jl:58 [inlined]
 [19] display(::AbstractPlotting.PlotDisplay, ::Scene) at /home/pietro/.ju
lia/packages/AbstractPlotting/AO27s/src/display.jl:45
 [20] display(::Any) at ./multimedia.jl:323
 [21] #invokelatest#1 at ./essentials.jl:712 [inlined]
 [22] invokelatest at ./essentials.jl:711 [inlined]
 [23] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at /buildworker/w
orker/package_linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:1
61
 [24] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at /build
worker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/R
EPL.jl:146
 [25] (::REPL.var"#do_respond#38"{Bool,REPL.var"#48#57"{REPL.LineEditREPL,
REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any,
::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia
/stdlib/v1.4/REPL/src/REPL.jl:729
 [26] #invokelatest#1 at ./essentials.jl:712 [inlined]
 [27] invokelatest at ./essentials.jl:711 [inlined]
 [28] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalIn
terface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linux64/b
uild/usr/share/julia/stdlib/v1.4/REPL/src/LineEdit.jl:2354
 [29] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildwo
rker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/REP
L.jl:1055
 [30] run_repl(::REPL.AbstractREPL, ::Any) at /buildworker/worker/package_
linux64/build/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:206
 [31] (::Base.var"#764#766"{Bool,Bool,Bool,Bool})(::Module) at ./client.jl
:383
 [32] #invokelatest#1 at ./essentials.jl:712 [inlined]
 [33] invokelatest at ./essentials.jl:711 [inlined]
 [34] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl
:367
 [35] exec_options(::Base.JLOptions) at ./client.jl:305
 [36] _start() at ./client.jl:484

@asinghvi17 asinghvi17 added the WIP Work in progress - do not merge! label Mar 14, 2020
@piever
Copy link
Member Author

piever commented Mar 14, 2020

Very far from being mergeable, but it works! You need piever/AlgebraOfGraphics.jl master.

Demo

using RDatasets: dataset

using StatsMakie: linear
using AbstractPlotting, GLMakie

using AlgebraOfGraphics: Data, Select, Group

mpg = dataset("ggplot2", "mpg");
cols = Data(mpg) * Select(:Displ, :Hwy);
grp = Group(color = :Cyl);

# I have pirated AbstractPlotting, so that plot types and attributes can be used too!
plot(Scatter * cols * Attributes(markersize=10px))

test

# Now I can simply add `grp` to do the grouping
plot(Scatter * cols * grp * Attributes(markersize=10px))

test

# This is almost a recipe with scatter and linear regression :)
# It can be applied to the arguments just by multiplying them
plots = Scatter * Attributes(markersize = 10px) + linear * Attributes(linewidth = 5)
plot(plots * cols)

test

# Again, if I multiply by the grouping, I add it to the scene
plot(plots * cols * grp)

test

# This is a more complex example, where I want to split the scatter,
# but do the linear regression with all the data
different_grouping = grp * Scatter * Attributes(markersize = 10px) + linear * Attributes(linewidth = 5)
plot(cols * different_grouping)

test

@piever
Copy link
Member Author

piever commented Mar 14, 2020

This implementation should actually live in AbstractPlotting eventually, so type piracy is not a concern if we can agree on the meaning of operators.

@piever
Copy link
Member Author

piever commented Mar 15, 2020

Following a discussion with @SimonDanisch, now from a Group and a Style it is possible to build a Traces object, which can also be passed by hand. It needs to iterate Pair{NamedTuple, Select}, where the named tuple acts as a "primary key". If there are multiple Traces objects, all the "consistent options" are kept (i.e. where the shared primary keys match). If the primary keys are "disjoint", all combinations are allowed. counter simply gives values from 1 to n to all the attributes that are passed to it as symbols.

Examples

julia> using AlgebraOfGraphics: Select, Traces

julia> ts1 = Select(rand(10));

julia> ts2 = Traces(counter(:color), eachcol(rand(10, 3)));

julia> plot(Scatter * ts1 * ts2, markersize = 10px)

test

julia> ts1 = Traces(counter(:marker), eachcol(rand(10, 3)));

julia> ts2 = Traces(counter(:color), eachcol(rand(10, 3)));

julia> plot(Scatter * ts1 * ts2, markersize = 10px)

test

julia> ts1 = Traces(counter(:color), [-pi..0, 0..pi]);

julia> ts2 = Traces(counter(:linestyle), [sin, cos]);

julia> plot(ts1 * ts2, linewidth = 10)

test

@piever piever closed this Mar 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
WIP Work in progress - do not merge!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants