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


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

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 for the keyword arguments.

1 Like

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

It’s pretty good! With

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

I get

and to get just jittered points,

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

Can also do clouds=hist:

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

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

1 Like

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

1 Like

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.

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
1 Like