# AlgebraOfGraphics question

**URL:** <https://discourse.julialang.org/t/algebraofgraphics-question/72982>\
**Category:** Visualization\
**Created:** [December 12, 2021, 4:48pm UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982 "2021-12-12T16:48:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 12, 2021, 4:48pm UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982/1 "2021-12-12T16:48:04Z")

</div>

In [Philosophy · Algebra of Graphics (juliaplots.org)](http://juliaplots.org/AlgebraOfGraphics.jl/stable/philosophy/)  
the authors explain that AlgebraOfGraphics doesn’t do mind reading and want to cut down the clutter/repetition when specifying a plot.  
I wonder what is the price to pay for their approach.

Don’t get me wrong, thanks for the work! The examples are amazing and useful.  
I am stumbling over the simple things.

I want to plot two lines with the same x-axis and a legend.  
There is no legend command, but using the search found an example doing what I wanted:

```julia
using AlgebraOfGraphics, GLMakie

N = 40

x = [1:N; 1:N]
y = [cumsum(randn(N)); cumsum(randn(N))]
grp = [fill("a", N); fill("b", N)]

# NOT working for a single line
#x = [1:N]
#y = [cumsum(randn(N))]
#grp = [fill("a", N)]

df = (; x, y, grp)

layers = visual(Lines) + visual(Scatter) * mapping(marker = :grp)
plt = data(df) * layers * mapping(:x, :y, color = :grp)

fg = draw(plt)

```

From the code I understand that each y value must have its own x value and its own legend.  
This looks allocation-heavy.  
Additionally, the name of the legend (grp here) appears on the plot. How to suppress it?

I thought of writing a wrapper, so first experimented to cut back the example to a single plot line, without success (plots the values as single data line, no graphics).

Is there a convenience function or what am I missing?  
Thx

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [December 12, 2021, 6:32pm UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982/2 "2021-12-12T18:32:59Z")

</div>

So, I think the issue you are running into is “long vs wide vs pre-grouped data format”. In particular,

> From the code I understand that each y value must have its own x value and its own legend.  
> This looks allocation-heavy.

seems to refer to “long form data”, which does incur in some repetition. Long-form data is in my understanding the default in e.g. ggplot2 and other Grammar of Graphics software. It definitely has its advantages but also some drawbacks.

One of the principles of AlgebraOfGraphics is that the plotting library should not enforce the data format, see [this philosophy section](http://juliaplots.org/AlgebraOfGraphics.jl/stable/philosophy/#Wide-format-support). In particular, it seems like what you are looking for is [pre-grouped data support](http://juliaplots.org/AlgebraOfGraphics.jl/stable/gallery/gallery/data%20manipulations/pre_grouped_data/#Pre-grouped-data). The exact syntax may change (see [this issue](https://github.com/JuliaPlots/AlgebraOfGraphics.jl/issues/308)), but you can already do e.g.

```julia
using AlgebraOfGraphics, GLMakie
N = 40
x = [1:N] # list of vectors for `x` axis
y = [cumsum(randn(N)), cumsum(randn(N))] # list of vectors for `y` axis
grp = ["a", "b"] # list of strings for groupings
layers = visual(Lines) + visual(Scatter) * mapping(marker = grp)
plt = layers * mapping(x, y, color = grp)
fg = draw(plt)

```

The different variables are combined with broadcast semantics, so `x = [1:N]` works the same as `x = [1:N, 1:N]` as it is expanded along a trailing singleton dimension.

To name axes or legends in this format you would use the pair syntax, that is `x => "x"`, `y => "y"`, `grp => "group"`.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 12, 2021, 7:52pm UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982/3 "2021-12-12T19:52:46Z")

</div>

Thanks for the example, works!  
The printing of the legend name `grp` is gone with `df = (; x, y, grp)` gone. Is this reasoning correct?  
Now how to adapt the box surrounding the legend as well?

Honestly, I do not recognize the given code in the [pre-grouped data support](http://juliaplots.org/AlgebraOfGraphics.jl/stable/gallery/gallery/data%20manipulations/pre_grouped_data/#Pre-grouped-data) section.  
Definitely need more study to understand the concepts behind.  
How to improve accessibility? Simple examples help for sure.  
Thx again.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [December 13, 2021, 11:18am UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982/4 "2021-12-13T11:18:01Z")

</div>

> [@Bardo](#):
>
> The printing of the legend name `grp` is gone with `df = (; x, y, grp)` gone. Is this reasoning correct?

Yes, AoG tries to infer the name, but if you pass the variable directly, it is unnamed by default (there is no way to know the name of the variable from the function).

> [@Bardo](#):
>
> Now how to adapt the box surrounding the legend as well?

I’m not sure there is an easy way at the moment (AoG simply removes the box altogether in its default style, so that may help). It would probably be a good “getting started” PR to set up the legend creation so that it calls the “title-free” MakieLayout legend method when the legend title is the empty string.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 13, 2021, 12:26pm UTC](https://discourse.julialang.org/t/algebraofgraphics-question/72982/5 "2021-12-13T12:26:16Z")

</div>

> [@piever](#):
>
> To name axes or legends in this format you would use the pair syntax, that is `x => "x"` , `y => "y"` , `grp => "group"` .

How to apply the pair syntax?  
I tried some possible places.  
VSCode flags where the syntax is obviously wrong, other locations give run-time errors.  
I searched but could not find code with `=>` in the tutorial or gallery examples.
