AlgebraOfGraphics.jl icon indicating copy to clipboard operation
AlgebraOfGraphics.jl copied to clipboard

Jitter (feature request)

Open jdm204 opened this issue 3 years ago • 5 comments

AlgebraOfGraphics is very nice!

I was wondering if support for jitter is planned, 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 visual like geom_jitter(width=0.4, height=0).

jdm204 avatar Aug 08 '21 13:08 jdm204

@kimlaberinto created a function for this:

function create_jitter_array(length_data_array; jitter_width = 0.1, clamped_portion = 0.1)
    jitter_width < 0 && ArgumentError("`jitter_width` should be positive.")
    !(0 <= clamped_portion <= 1) || ArgumentError("`clamped_portion` should be between 0.0 to 1.0")

    # Make base jitter, note base jitter minimum-to-maximum span is 1.0
    base_min, base_max = (-0.5, 0.5)
    jitter = [rand_localized(base_min, base_max) for _ in 1:length_data_array]

    # created clamp_min, and clamp_max to clamp a portion of the data
    @assert (base_max - base_min) == 1.0
    @assert (base_max + base_min) / 2.0 == 0
    clamp_min = base_min + (clamped_portion / 2.0)
    clamp_max = base_max - (clamped_portion / 2.0)

    # clamp if need be
    clamp!(jitter, clamp_min, clamp_max)

    # Based on assumptions of clamp_min and clamp_max above
    jitter = jitter * (jitter_width / clamp_max)

    return jitter
end

Maybe we can figure out how to upstream these... This was for a raincloud plot recipe, which might as well live in Makie itself...

SimonDanisch avatar Sep 28 '21 08:09 SimonDanisch

Just to support this request - in ggbeeswarm there are some nice examples what to use as the randomization function.

grafik

I personally have a strong preference for "quasirandom" - but no idea on the implementation side.

Taken from: https://cran.r-project.org/web/packages/ggbeeswarm/vignettes/usageExamples.pdf

behinger avatar Oct 25 '21 11:10 behinger

Just found out, Gadfly has beeswarm, maybe helpful. https://github.com/GiovineItalia/Gadfly.jl/blob/0bec09d30eca618e10c64fe383024211629c0c65/src/geom/beeswarm.jl#L10-L17

behinger avatar Jan 25 '22 09:01 behinger

This feature would be nice!

rud-lucien avatar Apr 11 '23 14:04 rud-lucien