# \[AlgebraOfGraphics.jl\] adding jitter to a scatter plot points

**URL:** <https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310>\
**Category:** Visualization\
**Created:** [April 10, 2023, 5:16pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310 "2023-04-10T17:16:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rud-lucien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rud-lucien/32/38576_2.png) [@rud-lucien](https://discourse.julialang.org/u/rud-lucien)\
**Post date:** [April 10, 2023, 5:16pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/1 "2023-04-10T17:16:03Z")

</div>

Hi,

I was wondering if anyone knows how (or if it is currently possible) to add jitter to the position of the points in this scatter plot generated with `AlgebarOFGraphics.jl`?

```
using CairoMakie, AlgebraOfGraphics, DataFrames, PalmerPenguins;

df = PalmerPenguins.load() |> DataFrame |> dropmissing;
	data(df) *
		mapping(:species, :bill_length_mm; color=:sex => nonnumeric) *
		visual(Scatter; markersize=10, alpha=0.5) |> draw

```

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

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 10, 2023, 10:28pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/2 "2023-04-10T22:28:21Z")

</div>

> <https://github.com/MakieOrg/AlgebraOfGraphics.jl/issues/253>
>
> AlgebraOfGraphics is very nice!
> 
> I was wondering if support for jitter is plan…ned, i.e. adding a bit of randomness to the positions of scatter points to help with overplotting.
> 
> For example, when plotting continuous data (y) against categorical (x) it can be nice to jitter the points in the horizontal x direction:
> 
> \`\`\`
> df = PalmerPenguins.load() |\> DataFrame |\> dropmissing
> data(df) \* mapping(:sex, :body\_mass\_g) \* (visual(BoxPlot) + visual(Scatter)) |\> draw
> \`\`\`
> 
> In ggplot2 for example this is implemented as parameters to the point visual and by a \[jitter\](https://ggplot2.tidyverse.org/reference/geom\_jitter.html) visual like \`geom\_jitter(width=0.4, height=0)\`.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 10, 2023, 11:52pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/3 "2023-04-10T23:52:15Z")

</div>

You could use `RainClouds` instead of `Scatter`. With `plot_boxplots=false` and `clouds=nothing` I think that is essentially what you want, but those options can be nice too. See [rainclouds · Makie](https://docs.makie.org/stable/examples/plotting_functions/rainclouds/index.html#rainclouds) for the keyword arguments.

---

<div class="post-metadata">

**Author:** ![rud-lucien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rud-lucien/32/38576_2.png) [@rud-lucien](https://discourse.julialang.org/u/rud-lucien)\
**Post date:** [April 11, 2023, 2:08pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/4 "2023-04-11T14:08:03Z")

</div>

Thanks for the suggestion. I wasn’t aware of the raincloud option in Makie. I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 11, 2023, 2:30pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/5 "2023-04-11T14:30:54Z")

</div>

It’s pretty good! With

```julia
data(df) * mapping(:species, :bill_length_mm; color=:sex => nonnumeric) *
                       visual(RainClouds) |> draw

```

I get

 ![rainclouds](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3741305a8c5b688e51745c3ce20d9ef746dc090.png)  
and to get just jittered points,

```julia
data(df) * mapping(:species, :bill_length_mm; color=:sex => nonnumeric) *
                       visual(RainClouds; clouds=nothing, plot_boxplots=false) |> draw

```

 ![plt](https://global.discourse-cdn.com/julialang/original/3X/0/3/031c226ee56b629b4a2d55970ecf683ee0e5f342.png)

Can also do `clouds=hist`:

```julia
data(df) * mapping(:species, :bill_length_mm; color=:sex => nonnumeric) *
                       visual(RainClouds; clouds=hist, plot_boxplots=false) |> draw

```

 ![plt](https://global.discourse-cdn.com/julialang/original/3X/3/4/347bc90300f0d825b74cefbbdbab32444c6bb46e.png)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 11, 2023, 2:39pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/6 "2023-04-11T14:39:50Z")

</div>

For what it’s worth, I think that these suggested plots look much better than just adding jitter to the original plot.

---

<div class="post-metadata">

**Author:** ![rud-lucien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rud-lucien/32/38576_2.png) [@rud-lucien](https://discourse.julialang.org/u/rud-lucien)\
**Post date:** [April 11, 2023, 2:42pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/7 "2023-04-11T14:42:16Z")

</div>

Agreed! The histogram, boxplot, and density make this much more useful.

---

<div class="post-metadata">

**Author:** ![rud-lucien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rud-lucien/32/38576_2.png) [@rud-lucien](https://discourse.julialang.org/u/rud-lucien)\
**Post date:** [April 11, 2023, 3:20pm UTC](https://discourse.julialang.org/t/algebraofgraphics-jl-adding-jitter-to-a-scatter-plot-points/97310/8 "2023-04-11T15:20:47Z")

</div>

Thanks again @ericphanson! I was actually able to generate the exact plot I was envisioning in my head with your `RainCloud` suggestion. Looks like the jitter functionality is tucked away in that function. Although I think the additional options the `RainCloud` function provides are much more useful, posting the jitter specific code here for others who might be looking for this type of plot in the future.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/8/48cd829478ce92b374d43e46fd566e1345a97999.png)

```julia
data(df) * mapping(:species, :bill_length_mm; color=:sex => nonnumeric) *
                       visual(RainClouds; markersize=10, jitter_width=0.25, alpha=0.5, clouds=nothing, plot_boxplots=false) |> draw

```
