# Bottom horizontal legend

**URL:** <https://discourse.julialang.org/t/bottom-horizontal-legend/62548>\
**Category:** General Usage\
**Tags:** makie, algebraofgraphics\
**Created:** [June 7, 2021, 8:51pm UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548 "2021-06-07T20:51:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 7, 2021, 8:51pm UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548/1 "2021-06-07T20:51:02Z")

</div>

```julia
layer = let
n = 100
df = (;
        x=rand(n),
        y=rand(n),
        c=rand(["a", "b", "c"], n),
    )
d = data(df)
m = mapping(:x, :y, color=:c)
v = visual(Lines)
d * m * v
end

grid = draw(layer)
leg = only(x for x in contents(grid.figure.layout) if x isa Legend)
leg.orientation = :horizontal
grid.figure.layout[2,1] = leg
leg.tellwidth = false
grid

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/9/b969557b920b44a2fda80820fb610b80f148c984.jpeg)

- How can I fix the plot width?
- Is there a way to build this horizontal bottom legend declaratively without all this stuff:

```julia
leg = only(x for x in contents(grid.figure.layout) if x isa Legend)
leg.orientation = :horizontal
grid.figure.layout[2,1] = leg

```

?

---

<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, 2021, 5:37am UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548/2 "2021-06-08T05:37:56Z")

</div>

I guess for this simple case, AlgebraOfGraphics should just offer a horizontal legend switch. I like it a lot that you can pull out the legend, move it, and restyle it, but of course that adds some code that you don’t want to write everytime.

Regarding the spacing question, that exact case is talked about in the layout tutorial [https://makie.juliaplots.org/stable/makielayout/tutorial.html#Fixing-spacing-issues](https://makie.juliaplots.org/stable/makielayout/tutorial.html#Fixing-spacing-issues)

You can trim away the now empty space and make the legend adjust its own row height down.

---

<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:** [June 8, 2021, 7:01pm UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548/3 "2021-06-08T19:01:22Z")

</div>

So at the moment `draw` does the plot call, adds facet styling, adds the legend and tries to “resize figure to fit”. I was planning to keep the `draw` interface reasonably simple, and if one wants more you would simply do these steps by hand and customize via Makie directly. (I hadn’t thought about moving it after having drawn it and did not know it was possible.)

That being said, horizontal legend may be of sufficiently general interest that one can just add a switch to `draw` directly.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 8, 2021, 7:57pm UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548/4 "2021-06-08T19:57:13Z")

</div>

I wonder if it would make sense to have a whole AoG algebraic function for configuring a legend. Legends are pretty flexible and underused for conveying semantics imo.

---

<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:26am UTC](https://discourse.julialang.org/t/bottom-horizontal-legend/62548/5 "2022-06-07T07:26:54Z")

</div>

As a service for future readers:

Customizing the legend has been simplified a few releases ago. You can use the `legend` keyword in `draw`. (see also [http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/customization/legend/#Legend-tweaking](http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/customization/legend/#Legend-tweaking))

```julia
begin
	fig = Figure()
	
	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)
	
	draw(plt, legend = (position = :bottom, titleposition = :left))
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/f/df53bf140d12c6e1ed855e7ad6ee026d2b1fc483.png)
