AlgebraOfGraphics.jl
AlgebraOfGraphics.jl copied to clipboard
Dodge in scatter,lines
Problem description
I often encountered the issue, that there is no way to dodge datapoints in a scatter (or less important imho line) plot.
That is going from here:
To here:
Something similar is asked here #230 I think
Proposed solution
I dont understand enough of the internals, but as I understand Makie.jl implements the dodge feature for violin & histograms.
In ggplot2 dodging is possible by specifing a position-function (position = position_dodge(width=0.2)
). I was expecting something like
data(df) * mapping(x,y,color="categoryA",dodge="categoryA") * visual(Scatter)
to "just work", by doding the colored datapoints.
Let's just hope I missed something ;-)
ok - I needed this again and couldnt find a solution. I implemented a new Makie Recipe Dodge
, which allows you to do exactly this.
Please find the gist here: https://gist.github.com/behinger/8df41a3e051979a8e8ee0068f1aac6b8
I don't really know whether @recipe Dodge
should be rather in Makie.jl
@SimonDanisch what do you think?
I also dont know if this is the nicest way, but one can dodge all kinds of plots. tried with scatter and errorbars. dodging in y instead of x is supported as well.
MWE
begin
using Makie
using CairoMakie
using HTTP
HTTP.download("https://gist.githubusercontent.com/behinger/8df41a3e051979a8e8ee0068f1aac6b8/raw/4c6f44603d4432b19c8abe2955eab4962a8ae45f/MakieDodge.jl",tempdir()) |>include
#----
x = [1,2,3, 1,2,3., 3,2,1]
y = [1.1,1.9,2.8,1,2,3,3.05,2.05,1.05]
c = ["red","red","red","green","green","green","blue","blue","blue"]
low,high = repeat([0.2],length(x))
dodge(x,y,low,high;plot_fun=errorbars!,color=c)
dodge!(x,y;color=c, plot_fun=scatter!)
current_figure()
end
ah and of course with AoG
let
d = DataFrame(:x=>["x2","x1","x2","x1","x3","x3","x4"],:y=>[2.,1.1,2.1,1.,3.,3.1,4],:c=>["a","a","b","b","b","b","b"])
d.high = repeat([0.1],nrow(d))
d.low = repeat([0.1],nrow(d))
data(d)*
mapping(color=:c,dodge=:c) *
(
mapping(:x,:y,:low,:high,)* visual(Dodge,plot_fun=errorbars!) +
mapping(:x,:y) * visual(Dodge)
) |> draw
end