# \[AlgebraOfGraphics.jl\] place legend inside figure

**URL:** https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991
**Category:** Visualization
**Tags:** makie, algebraofgraphics
**Created:** [May 31, 2022, 1:47pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991 "2022-05-31T13:47:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [May 31, 2022, 1:47pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/1 "2022-05-31T13:47:18Z")

</div>

Is it possible to place legends inside figures with `AlgebraOfGraphics.jl`? I am looking for a functionality similar to what `Makie.jl`’s `axislegend` function provides.

---

<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: [June 4, 2022, 12:41pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/2 "2022-06-04T12:41:42Z")

</div>

Hello @TheLateKronos, have you found a solution to this problem?

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [June 6, 2022, 8:31am UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/3 "2022-06-06T08:31:43Z")

</div>

Nope.

---

<div class="post-metadata">

### Author: ![fabgrei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabgrei/32/4214_2.png) [@fabgrei](https://discourse.julialang.org/u/fabgrei)
#### Post date: [June 7, 2022, 7:06am UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/4 "2022-06-07T07:06:49Z")

</div>

```julia
begin
	using CairoMakie, AlgebraOfGraphics
	
	df = (
	    x=rand(500),
	    y=rand(500),
	    k=rand(["a", "b", "c"], 500),
	    l=rand(["d", "e", "f"], 500),
	)
	plt = data(df) * mapping(:x, :y, color=:k, marker=:l) * visual(Scatter)

	# use `draw!` to draw into an existing figure
	fig = Figure()
	ag = draw!(fig[1,1], plt)
	
	# use internal function to compute legend components
	leg = AlgebraOfGraphics.compute_legend(ag)

	# add standard legend
	Legend(fig[1,2], leg...)

	# add axis legend
	ax = fig[1,1] |> contents |> first |> contents |> first # get the axis
	axislegend(ax, leg...)

	fig
end

```

![image](https://global.discourse-cdn.com/julialang/original/3X/0/3/0346bb41351c7bab987557b70b7389b2fa110c78.png)

---

<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: [June 7, 2022, 3:13pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/5 "2022-06-07T15:13:02Z")

</div>

Thank you very much @fabgrei and all this community!

 ![plot_111](https://global.discourse-cdn.com/julialang/original/3X/1/5/159e1f47d5b5282be8128be8b8966460c947849e.png)

---

<div class="post-metadata">

### Author: ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)
#### Post date: [June 7, 2022, 6:26pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/6 "2022-06-07T18:26:00Z")

</div>

I could be misunderstanding the use case here, but would just using `legend!` work as well?

```julia
using CairoMakie, AlgebraOfGraphics

df = (
    x=rand(500),
    y=rand(500),
    k=rand(["a", "b", "c"], 500),
    l=rand(["d", "e", "f"], 500),
)

plt = data(df) * mapping(:x, :y, color=:k, marker=:l) * visual(Scatter)

fig = Figure()
gridpos = fig[1, 1]

f = draw!(gridpos, plt)
legend!(gridpos, f; tellwidth=false, halign=:right, valign=:top, margin=(10, 10, 10, 10))

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6b6207871aab54987e852c98e127a4f4275208c.png)

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 7, 2022, 7:13pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/7 "2022-06-07T19:13:31Z")

</div>

And you can set a padding on the legend as well (or margin, I think that one’s the outside)

---

<div class="post-metadata">

### Author: ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)
#### Post date: [June 7, 2022, 7:34pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/8 "2022-06-07T19:34:33Z")

</div>

Thanks, took a peek at `axislegend` to see what values were being used and added it to the example

btw, would it make sense to add a `position` keyword for `Legend` to have it more closely match `axislegend`? Using `position=:rt` instead of `halign=:right, valign=:top` is really nice

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 8, 2022, 7:42am UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/9 "2022-06-08T07:42:14Z")

</div>

I’m starting to doubt that `position` was a good idea, I thought it was a nice shortcut at the time and was inspired by other packages, but the align thing is much simpler. Although it would be an option to go through the motions and refactor GridLayoutBase so it uses only one `align` instead of `halign` and `valign`, and then one could do `align = :cc` or so as a shortcut. Would be pretty breaking though.

---

<div class="post-metadata">

### Author: ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)
#### Post date: [June 8, 2022, 3:07pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-place-legend-inside-figure/81991/10 "2022-06-08T15:07:44Z")

</div>

Oh, I thought `position` just passed the corresponding `halign`/`valign` to `Legend` via [`legend_position_to_aligns`](https://github.com/JuliaPlots/Makie.jl/blob/089373ef1183ff355f457f2a521d53c256af7b97/src/makielayout/blocks/legend.jl#L602-L625)? Do you mean that this is probably more trouble than it’s worth for supporting other types of positioning, like in this issue here for example? [`axislegend` cannot be positioned using a Tuple of numbers · Issue #2006 · JuliaPlots/Makie.jl · GitHub](https://github.com/JuliaPlots/Makie.jl/issues/2006)
