AlgebraOfGraphics force points colors in scatter plot

I starting using AlgebraOfGraphics and I really like it so far.

Now I’m using it in a Pluto notebook to plot two variables as a scatter colored by groups with CairoMakie as a backend. I also added some checkboxes to make the plot interactive so the user can choose which groups are plotted or not. But I would like to have a mapping between my group values and a color so the color of a group does not change based on which other group is plotted or not.

I know we can change the color palette using the palettes argument thanks to this question/answer, but what I want is choosing which color is associated to which group in my data.

I tried computing the color beforehand and pass it as an array to the color argument in the mapping and in the palette argument of draw, and I tried to pass a Dict mapping my group names to the color value as a palette. But none of these worked.

I know I could use another back-end to make the plots interactive (e.g. the WGLMakie), but I would prefer using CairoMakie because other actions are triggered on the group choice too.

For those familiar with ggplot I would simply like to use something similar to:

[...]
scale_color_manual(values = c("setosa" = "purple", "versicolor" = "orange", "virginica" = "steelblue"))

Try this:

using AlgebraOfGraphics
using ColorSchemes
using SomethingMakie

colors=[
	"Adelie" => ColorSchemes.Set1_3.colors[1],
	"Chistrap" => ColorSchemes.Set1_3.colors[2],
	"Gentoo" => ColorSchemes.Set1_3.colors[3]
	]

fig = data(penguins) *
	mapping(
		:bill_length_mm,
		:bill_depth_mm;
		col=:sex,
		color=:species) * 
	(linear() * visual(linewidth=3) +
		mapping())

draw(
	fig;
	palettes=(; color=ColorSchemes.Set1_3.colors)
		)

Thanks for the answer! However I do not understand where the colors Array is used after declaration ? This one:

colors=[
	"Adelie" => ColorSchemes.Set1_3.colors[1],
	"Chistrap" => ColorSchemes.Set1_3.colors[2],
	"Gentoo" => ColorSchemes.Set1_3.colors[3]
	]

Because if I use your code like this the color is not really mapped to the group unfortunately.

Take a look at the AoG documentation example: http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/scales/custom_scales/

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" => ColorSchemes.Set1_4.colors[1],
               "b" => ColorSchemes.Set1_4.colors[2],
               "c" => ColorSchemes.Set1_4.colors[3],
               "d" => ColorSchemes.Set1_4.colors[4]
]
draw(plt; palettes=(color=colors,))

Or a more real-world example:

using AlgebraOfGraphics
using ColorSchemes
using SomethingMakie

colors=[
	"Adelie" => ColorSchemes.Set1_3.colors[1],
	"Chinstrap" => ColorSchemes.Set1_3.colors[2],
	"Gentoo" => ColorSchemes.Set1_3.colors[3]
	]

fig = data(penguins) *
	mapping(
		:bill_length_mm,
		:bill_depth_mm;
		col=:sex,
		color=:species) * 
	(linear() * visual(linewidth=3) +
		mapping())

draw(
	fig;
	palettes=(; color=colors)
		)

2 Likes

Ha perfect thank you! I missed this one on the docs (and I looked to the stable docs).

1 Like