# AlgebraOfGraphics force points colors in scatter plot

**URL:** <https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089>\
**Category:** Visualization\
**Tags:** makie, algebraofgraphics\
**Created:** [August 27, 2021, 9:37am UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089 "2021-08-27T09:37:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvezy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvezy/32/17435_2.png) [@rvezy](https://discourse.julialang.org/u/rvezy)\
**Post date:** [August 27, 2021, 9:37am UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089/1 "2021-08-27T09:37:46Z")

</div>

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](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335), 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:

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

```

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [August 27, 2021, 9:57am UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089/2 "2021-08-27T09:57:13Z")

</div>

Try this:

```julia
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)
		)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a113d5ed7568967fa1602ec2dc84e12636d59f5.png)

---

<div class="post-metadata">

**Author:** ![rvezy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvezy/32/17435_2.png) [@rvezy](https://discourse.julialang.org/u/rvezy)\
**Post date:** [August 27, 2021, 10:20am UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089/3 "2021-08-27T10:20:36Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [August 27, 2021, 11:09am UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089/4 "2021-08-27T11:09:08Z")

</div>

Take a look at the AoG documentation example: [http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/scales/custom\_scales/](http://juliaplots.org/AlgebraOfGraphics.jl/dev/gallery/gallery/scales/custom_scales/)

```julia
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,))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/2/52b34fd9cdda346a3c49e31dfd7054a736ccc6cf.png)

Or a more real-world example:

```julia
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)
		)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/4/540e10ff70940241a4e67c442ad705db99f8cd96.png)

---

<div class="post-metadata">

**Author:** ![rvezy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvezy/32/17435_2.png) [@rvezy](https://discourse.julialang.org/u/rvezy)\
**Post date:** [August 27, 2021, 1:16pm UTC](https://discourse.julialang.org/t/algebraofgraphics-force-points-colors-in-scatter-plot/67089/5 "2021-08-27T13:16:44Z")

</div>

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