Skip to content

Legend options

julia
using AlgebraOfGraphics, CairoMakie

To tweak the position and appearance of the legend, simply use the legend keyword when plotting. For example

julia
labels = ["a looooooong label", "an even loooooonger label", "and one more long label"]
df = (x=rand(100), y=rand(100), group=rand(labels, 100))
layers = linear() + mapping(color=:group)
plt = data(df) * layers * mapping(:x, :y)
draw(plt)

julia
fg = draw(plt, legend=(position=:top, titleposition=:left, framevisible=true, padding=5))

To adjust the title and order of labels in a legend you can use the pair syntax.

julia
layers = linear() +  mapping(color=:group => sorter(labels) => "Labels")
plt = data(df) * layers * mapping(:x, :y)
draw(plt)

Adding a plot to a pre-existing figure with draw! will not draw the legend automatically. In this case, one must use legend! and specify the axis to which it should be added.

The tellheight = false, tellwidth = false arguments are useful to avoid changing the dimensions of the axis.

julia
makie_fig = Figure()
ax_scatter = Axis(makie_fig[1, 1])

grid = draw!(ax_scatter, plt)

legend!(makie_fig[1, 1], grid; tellheight=false, tellwidth=false, halign=:right, valign=:top)

makie_fig

If the automatic legend elements are not legible enough, you can change their properties by passing overrides to the legend attribute of a visual.

julia
df = (;
    x = repeat(1:100, 5),
    y = reduce(vcat, [[cos(x) for x in range(0, 8pi, length = 100)] .+ 0.3 .* randn.() for _ in 1:5]),
    group = repeat(1:5, inner = 100),
)

lin = data(df) *
    mapping(:x, :y, group = :group => nonnumeric) *
    visual(Lines, linewidth = 0.3, label = "Lines", legend = (; linewidth = 1.5))
sca = data(df) *
    mapping(:x, :y => y -> y + 5, group = :group => nonnumeric) *
    visual(Scatter, markersize = 3, label = "Scatter", legend = (; markersize = 12))

draw(lin + sca)

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.