AlgebraOfGraphics.jl icon indicating copy to clipboard operation
AlgebraOfGraphics.jl copied to clipboard

Dodge in scatter,lines

Open behinger opened this issue 2 years ago • 2 comments

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: image To here: image

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 ;-)

behinger avatar May 20 '22 08:05 behinger

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. image

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

behinger avatar Feb 05 '23 13:02 behinger

ah and of course with AoG image

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

behinger avatar Feb 05 '23 13:02 behinger