Makie Categorical colors

How to map a categorical column to a categorical color palette in Makie?

using CairoMakie,AlgebraOfGraphics,RDatasets
diamonds = dataset("ggplot2","diamonds")

In AoG it’s just

data(diamonds) * mapping(:Carat, :Price; color=:Color) * visual(Scatter) |> draw

but how to do it in Makie?

julia> scatter(diamonds.Carat, diamonds.Price; color=string.(diamonds.Color))
ERROR: Unknown color: E

I thought “surely Categorical should work”, but surprisingly it doesn’t:

julia> scatter(rand(100), rand(100), color=Categorical(rand("abc", 100)))
ERROR: MethodError: no method matching to_color(::Categorical)

Probably a bug, as this

scatter(Categorical(rand("abc", 100)))

works.

1 Like

If I pass color= strings it tries to parse them as color names and passing CategoricalArrays.CategoricalVector fails, but passing numbers makes a plot.

That doesn’t seem like an ideal API in the sense that color names are very different from values to be mapped into colors so I wouldn’t expect them to be accepted by the same kwarg.

julia> scatter(
    diamonds.Carat, 
    diamonds.Price; 
    color=levelcode.(diamonds.Cut), 
    colormap=Makie.wong_colors(),
)

Though is it me or is there too much yellow there, maybe it’s somehow treating it as continuous rather than categorical?