How to make a plot with multiple titles and labels in Makie

Question from Slack, posting here to keep the solution searchable in the future:

This is a figure from IMF blog. Is it possible to create a plot like this: Have title and a write up in the top and caption at the bottom

Here’s my attempt to make something similar in Makie.jl. Because there are three levels of title, I did this with an extra GridLayout but in the future, one will probably be able to change fonts within strings and just use title and subtitle attributes for this directly:

using CairoMakie
using Random
Random.seed!(1234)

x = range(2007, 2035, length = 1000)

function randdata(n)
    data = cumsum(randn(1000))
    data .-= range(first(data), last(data), length = 1000)
    data .* 0.1
end
euro = randdata(1000)
japan = randdata(1000) .+ 1
uk = randdata(1000) .- 2
emerging = randdata(1000) .- 6

f = Figure()
ax = Axis(f[1, 1], xgridvisible = false, ygridvisible = false, xticksvisible = false, alignmode = Outside(), xticks = 2007:4:2031, ytrimspine = true, 
    palette = (; color = [:deepskyblue, :firebrick3, :dodgerblue4, :goldenrod1]))

hidespines!(ax, :b, :t, :r)
hlines!(ax, 0, color = :black, linewidth = 1)

for (label, data) in ["Euro" => euro, "Japan" => japan, "UK" => uk, "Emerging markets" => emerging]
    lines!(ax, x, data; label, linewidth = 2)
end

titlelayout = GridLayout(f[0, 1], halign = :left, tellwidth = false)
Label(titlelayout[1, 1], "Interest rate differentials", halign = :left, textsize = 30, font = "TeX Gyre Heros Bold Makie")
Label(titlelayout[2, 1], "Differences in monetary policy are a key driver of the strong dollar.", halign = :left, textsize = 20)
Label(titlelayout[3, 1], "(versus US interest rate, percent)", halign = :left)
rowgap!(titlelayout, 0)

for (label, (year, y)) in ["Global\nfinancial\ncrisis" => (2008, 8), "Taper\ntantrum" => (2013, 8), "China\n2015\ncrisis" => (2015.4, 6), "Russia invasion\nof Ukraine" => (2022.1, 8)]
    lines!(ax, [year, year], [-10, y], color = :black, linestyle = :dash)
    text!(ax, year, y, text = label, align = (:left, :top), offset = (10, 0), textsize = 14)
end

ylims!(ax, -11, 11)
xlims!(ax, 2007, 2035)

axislegend(position = (0.5, 1.05), orientation = :horizontal,
    framevisible = false)

Label(f[2, 1], """
Sources: Bloomberg Finance L.P.; Haver Analytics; and IMF staff calculations.
Notes: Differential is calculated as US overnight bank funding rate minus foreign
inter-bank rate. The dots depict the forward paths for nominal interest rates.
""", tellwidth = false, halign = :left, justification = :left)

f
9 Likes

can this be added to beautiful makie?

1 Like

now it will be :smile: