ABLines in Algebraofgraphics (x=y line, identity line, 1-to-1 line)

Dear all,
I am new to Julia and AoG - coming from the Tidyverse and ggplot… - and don’t (even) manage to add an x=y line to a scatter plot:

using AlgebraOfGraphics, CairoMakie, Statistics
x=rand(4)
y = x .+ 0.2 .+ 0.2randn(4)
lab = string.(1:4)
p = data((;x, y, lab)) * mapping(:x, :y, color=:lab)

# This draws the "data"
draw(p, axis=(aspect=1, limits=((-0.2,1.4),(-0.2,1.4))))

# This would be my naive guess to include the abline
draw(p + visual(ABLines, slope=1.0, intercept=0.0), axis=(aspect=1, limits=((-0.2,1.4),(-0.2,1.4))))

# Thought I need to bind it better to the plot, but (of course) it interprets every x,y as slope, intercept... 
draw(p + p * visual(ABLines, linestyle=:dash, linewidth=5), axis=(aspect=1, limits=((-0.2,1.4),(-0.2,1.4))))

# Create a new dataset (would be cumbersome for this simple task, but also does not work)
p2 = data((;slope=[1.], intercept=[0.0])) * visual(ABLines, linestyle=:dash, linewidth=5)
p + p2; ### Ok
draw(p+p2; axis=(aspect=1,xticks=0:0.2:1.4, yticks=0:0.2:1.4, limits=((0,1.4),(0,1.4)))) 

The error I get is: ## ERROR: MethodError: no method matching combine_axes().

Something like this works, but is a horrible workaround (and does not plot just one segment of course):

p3 = data((;x, y, lab)) * mapping((:x, :y) => ((t1,t2) -> mean([t1,t2])), (:x, :y) => ((t1, t2) -> mean([t1, t2])) ) * visual(Lines, color=:red, linestyle=:dot)
draw(p+p3; axis=(aspect=1,xticks=0:0.2:1.4, yticks=0:0.2:1.4, limits=((0,1.4),(0,1.4))))

Thanks in advance for helping me not give up…

1 Like

You can specify data in mapping already, although I honestly don’t completely grok the logic of that. For example, mapping(x, y, color = lab) in the first part, without data, doesn’t work.

https://aog.makie.org/stable/gallery/gallery/data%20manipulations/pre_grouped_data/#Pre-grouped-data

Anyway, this syntax works if you don’t want to do a second data call for the ABLines:

julia> p = data((; x, y, lab)) * mapping(:x, :y, color = :lab) + mapping([0], [1]) * visual(ABLines);

julia> draw(p; axis=(aspect=1,xticks=0:0.2:1.4, yticks=0:0.2:1.4, limits=((0,1.4),(0,1.4))))

Thanks! This works and with defining

one2one =  mapping([0], [1]) * visual(ABLines, color=:red, linestyle=:dash)

beforehand (globally) I can use p + one2one , getting closer to ggplot style :wink:

I hope some of such convenience wrappers will be developed.