Custom scales

Source code Author

using AlgebraOfGraphics, CairoMakie
using Colors

A palette maps particular values to particular attribute specifications (e.g. 1 maps to green, 2 maps to red). Sometimes, there is no default palette for a specific attribute, and you will need to specify it manually, but there are sensible default palettes for many attributes. In either case, you can always manually specify the palette used for a particular attribute.

TODO: allow legend to use custom attribute of plot, such as the arrowhead or the arrowcolor and pass correct legend symbol.


A related concept (from Makie) is a colormap, which maps a continuous space of numbers to a sequence of colors. For discrete colors you will want to employ a palette, not a colormap, because discrete values are mapped to colors within AlgebraOfGraphics. AlgebraOfGraphics doesn't directly handle continuous colors: this is a feature of the underlying Makie plots.

x=repeat(1:20, inner=20)
y=repeat(1:20, outer=20)
c=rand(Bool, length(x))
d=rand(Bool, length(x))
df = (; x, y, u, v, c, d)
colors = [colorant"#E24A33", colorant"#348ABD"]
heads = ['▲', '●']
plt = data(df) *
    mapping(:x, :y, :u, :v) *
    mapping(arrowhead=:c => nonnumeric) *
    mapping(arrowcolor=:d => nonnumeric) *
    visual(Arrows, arrowsize=10, lengthscale=0.3)
draw(plt; palettes=(arrowcolor=colors, arrowhead=heads))

To associate specific attribute values to specific data values, use pairs. Missing keys will cycle over values that are not pairs.

x = rand(100)
y = rand(100)
z = rand(["a", "b", "c", "d"], 100)
df = (; x, y, z)
plt = data(df) * mapping(:x, :y, color=:z)
colors = ["a" => colorant"#E24A33", "c" => colorant"#348ABD", colorant"#988ED5", colorant"#777777"]
draw(plt; palettes=(color=colors,))

Categorical color gradients can also be passed to palettes.

x = rand(200)
y = rand(200)
z = rand(["a", "b", "c", "d", "e", "f", "g", "h"], 200)
df = (; x, y, z)
plt = data(df) * mapping(:x, :y, color=:z)
colors = cgrad(:cividis, 8, categorical=true)
fg = draw(plt; palettes=(color=colors,))

This page was generated using DemoCards.jl and Literate.jl.