# Behaviour of legend in AlgebraOfGraphics.jl

**URL:** https://discourse.julialang.org/t/behaviour-of-legend-in-algebraofgraphics-jl/101371
**Category:** Visualization
**Tags:** question
**Created:** [July 8, 2023, 10:04pm UTC](https://discourse.julialang.org/t/behaviour-of-legend-in-algebraofgraphics-jl/101371 "2023-07-08T22:04:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)
#### Post date: [July 8, 2023, 10:04pm UTC](https://discourse.julialang.org/t/behaviour-of-legend-in-algebraofgraphics-jl/101371/1 "2023-07-08T22:04:23Z")

</div>

Hi, I have made a MWE to show the behaviour I would like to avoid. I am plotting two curves, one of them with points and errobars, the other with lines. There are two problems. When avoiding the errorbars, in the legend the items appear with both points and lines. When plotting the errorbars, the items appear with polygons (rectangles) instead of the correct markers. Maybe I have some error in the mapping?  
Here is the MWE of the second case:

```julia
n = 20
df_a = DataFrame(grp=fill("a", n), x=collect(1:n), y=fill(2, n))
df_b = DataFrame(grp=fill("b", n), x=collect(1:n), y=fill(3, n), z=fill(0.3, n))

set_aog_theme!()
size_inches = (6.2*2, 3*2)
size_pt = 72 .* size_inches
fig = Figure(resolution = size_pt, fontsize = 30)
gridpos = fig[1, 1]
plt_a = data(df_a) *
    mapping(:x => L"x", :y=> L"y"; color = :grp => "", marker=:grp=> "", linestyle = :grp => "") *
    visual(Lines, linewidth=lw)
plt_b = data(df_b) *(
    mapping(:x => L"x", :y=> L"y"; color = :grp => "", marker=:grp=>"", linestyle= :grp => "") *
    visual(Scatter)+
    mapping(:x => L"x",:y=> L"y",:z;color=:grp=> "", marker=:grp=> "", linestyle = :grp => "")*visual(Errorbars))
f = draw!(gridpos, plt_a+plt_b, axis=(;limits=((0,n),(0,4)),
    xgridvisible=false, ygridvisible=false))
legend!(gridpos, f; tellwidth=false, halign=:center, valign=:bottom, margin=(10, 10, 10, 10), patchsize=(20,10))

display(fig)
leg = fig.content[2]
leg_items = leg.blockscene.children[1].plots
display(leg_items)

```

When displaying `fig.content[2].blockscene.childer[1].plots` it can be seen that there are 3 polygons. I think that the first one is correct, but not the other two.  
Here is the plot for the second case.  
![image|690x333](upload://lVVF5c2GCPhJmnKJv73rpnoCu1z.png  
Here is the plot for the first case (without errobars):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/d/adbbf4492ca882f5c0e908d40b83947124e78783.png)  
There it can be seen that in the legend `a` shouldn’t have a point while` b` should’t have the line segment.  
Thank you very much.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 10, 2023, 11:51am UTC](https://discourse.julialang.org/t/behaviour-of-legend-in-algebraofgraphics-jl/101371/2 "2023-07-10T11:51:53Z")

</div>

Here is a somewhat more minimal MWE that’s easier to experiment with:

```julia
using AlgebraOfGraphics, CairoMakie

n = 20
df_a = DataFrame(grp=fill("a", n), x=collect(1:n), y=fill(2, n))
df_b = DataFrame(grp=fill("b", n), x=collect(1:n), y=fill(3, n), z=fill(0.3, n))

set_aog_theme!()

xy_map = mapping(:x, :y);
xyz_map = mapping(:x,:y,:z);
fmt_map = mapping(color = :grp => "", linestyle = :grp => "", marker=:grp=> "");

plt_a = data(df_a) * fmt_map * xy_map * visual(Lines);
plt_b = data(df_b) * fmt_map * xy_map * visual(Scatter);
plt_eb = data(df_b) * fmt_map * xyz_map * visual(AlgebraOfGraphics.Errorbars);

fig = draw(plt_a + plt_b + plt_eb)

# Squares in legend, even if just the errorbars are plotted
# fig = draw(plt_eb)

```

Unfortunately, my experimentation did not produce an answer. Hopefully someone with more expertise in Makie can figure this out.

---

<div class="post-metadata">

### Author: ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)
#### Post date: [July 10, 2023, 5:13pm UTC](https://discourse.julialang.org/t/behaviour-of-legend-in-algebraofgraphics-jl/101371/3 "2023-07-10T17:13:53Z")

</div>

Thank you very much for trying to solve the problem and for improving the MWE. I think that what is happening is that if one plots df\_a with lines and df\_b with points, in the legend it displays both points and lines in both items.  
For example, lets look at the even simpler MWE based on @hendri54’s.

```julia
using AlgebraOfGraphics, CairoMakie

n = 20
df_a = DataFrame(grp=fill("a", n), x=collect(1:n), y=fill(2, n))
df_b = DataFrame(grp=fill("b", n), x=collect(1:n), y=fill(3, n), z=fill(0.3, n))

set_aog_theme!()

xy_map = mapping(:x, :y);
xyz_map = mapping(:x,:y,:z);
fmt_map = mapping(color = :grp => "", linestyle = :grp => "", marker=:grp=> "");

plt_a = data(df_a) * fmt_map * xy_map * visual(Lines);
plt_b = data(df_b) * fmt_map * xy_map * visual(Scatter);
plt_eb = data(df_b) * fmt_map * xyz_map * visual(AlgebraOfGraphics.Errorbars);

fig = draw(plt_a + plt_b)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/84a0a2b1cbd11931657c48ceb324662473923cb5.png)

If additionally one plots errorbars it also adds the rectangles in the legend. I have tested it by printing the field `fig.content[2].blockscene.childer[1].plots`.  
I have also tried removing the kwarg `linestyle=:grp` for` plt_b` and removing `marker:grp` for `plt_a`, but in that way it gives error.  
Thanks a lot for any help.
