# Figure title for a faceted plot in AlgebraOfGraphics

**URL:** <https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329>\
**Category:** General Usage\
**Tags:** question, plotting, algebraofgraphics\
**Created:** [June 21, 2021, 8:25pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329 "2021-06-21T20:25:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 21, 2021, 8:25pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/1 "2021-06-21T20:25:46Z")

</div>

I can’t seem to get an overall figure title for a faceted plot in AlgebraOfGraphics:

```julia
using CairoMakie
using AlgebraOfGraphics
using DataFrames

df = DataFrame(
    x = randn(100),
    y = randn(100),
    g = rand(["a", "b", "c", "d"], 100)
)

plt = data(df) * mapping(:x, :y, layout = :g)

```

Both of the following make a figure with no overall title:

```julia
draw(plt; axis = (; title = "hello world"))
draw(plt; figure = (; title = "hello world"))

```

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

Does anyone know how to do this?

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [June 21, 2021, 8:41pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/2 "2021-06-21T20:41:44Z")

</div>

Mmm, not an expert in AoG, but I would use this:  
[http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/gallery/#Embedding-facets](http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/gallery/#Embedding-facets)

with something like

```julia
ax = Axis(fig[0, 1:end], title="Figure title")

```

---

<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 21, 2021, 8:43pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/3 "2021-06-21T20:43:41Z")

</div>

I’ve done this before

```julia
title = Label(aog_object.figure, titletext, textsize=42, tellwidth=false)
aog_object.figure[0, 1:end] = title

```

---

<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 21, 2021, 9:10pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/4 "2021-06-21T21:10:39Z")

</div>

There’s currently nothing like a dedicated super title because it’s not clear how exactly that should work for all possible cases. The suggested solutions with manually placed labels are very flexible. Sometimes it can be a bit annoying to find out which indices to place the label at

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 21, 2021, 9:12pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/5 "2021-06-21T21:12:11Z")

</div>

Thanks! I think I’ll go with @jzr’s approach. For completeness, here’s the full MWE:

```julia
using CairoMakie
using AlgebraOfGraphics
using DataFrames

set_aog_theme!()

df = DataFrame(
    x = randn(100),
    y = randn(100),
    g = rand(["a", "b", "c", "d"], 100)
)

layer = data(df) * mapping(:x, :y, layout = :g)

plt = draw(layer)
plt.figure[0, :] = Label(plt.figure, "Hello World!", textsize=20)

```

---

<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 21, 2021, 9:19pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/6 "2021-06-21T21:19:29Z")

</div>

You can just do `Label(plot.figure[row, col],...)`

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 21, 2021, 9:21pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/7 "2021-06-21T21:21:18Z")

</div>

Does that mutate the figure object? I’m a bit befuddled by the way Makie mutates some of the plot objects that are arguments to constructors, with no exclamation point to signal the mutation… But maybe I’m misunderstanding something—I haven’t studied Makie in depth yet.

---

<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 21, 2021, 9:24pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/8 "2021-06-21T21:24:16Z")

</div>

Every layoutable has to mutate the figure if it’s placed in it, there’s no ! version because there is no alternative non mutating version. The syntax I just used just combines adding the object to the figure and putting it in a specific layout position, which your syntax splits in two

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 21, 2021, 9:31pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/9 "2021-06-21T21:31:38Z")

</div>

Ok. It just feels a bit odd to me. With this syntax, it’s clear that you are mutating the figure object:

```julia
plt.figure[0, :] = Label(plt.figure, "Hello World!", textsize=20)

```

But with your syntax,

```julia
Label(plot.figure[row, col],...)

```

it feels a bit like magical manipulation of global state, like the way that plotting works in Matlab.

In fact, I wonder why we can’t just do this:

```julia
# No figure first argument to Label:
plt.figure[0, :] = Label("Hello World!", textsize=20)

```

It seems like I ought to be able to make a `Label` object that is independent of any `Figure`, and then insert it into any figure that I care to use it in.

---

<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 21, 2021, 9:34pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/10 "2021-06-21T21:34:55Z")

</div>

That’s not possible with the current architecture as the label’s underlying text object can’t exist without a scene to be plotted into.

Indexing into a figure creates a FigurePosition object, and giving that as the first argument for a layoutable just does both the adding to the figure and adding to the layout. It’s nice because it works with nested layouts as well so you can pass nested figure positions to functions creating custom sublayouts and it usually just works.

---

<div class="post-metadata">

**Author:** ![rashid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rashid/32/9398_2.png) [@rashid](https://discourse.julialang.org/u/rashid)\
**Post date:** [July 23, 2024, 5:05pm UTC](https://discourse.julialang.org/t/figure-title-for-a-faceted-plot-in-algebraofgraphics/63329/11 "2024-07-23T17:05:26Z")

</div>

When I tried adding a title using `Label` the figure was getting clipped. For anyone facing this, this can be fixed by doing `resize_to_layout!(aog_object.figure)`
