# Align boxplot and scatter points in a rainclouds plot (Makie.jl)

**URL:** <https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [April 30, 2024, 11:25pm UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675 "2024-04-30T23:25:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheHedgehog2000](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@TheHedgehog2000](https://discourse.julialang.org/u/TheHedgehog2000)\
**Post date:** [April 30, 2024, 11:25pm UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/1 "2024-04-30T23:25:13Z")

</div>

Pretty specific question: is it possible to align the boxplot and scatter components of a rainclouds plot and have them centered on the xticks with Makie.jl? Basically, I am looking to overlay a boxplot and jitter plot and this seems to be the only way to do so in Julia. I realize this is not necessarily the intended use-case of the rainclouds plot.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 1, 2024, 12:43am UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/2 "2024-05-01T00:43:35Z")

</div>

it would be easier to help you if you can show what’s the desired outcome (maybe a plot you’ve seen somewhere else), and also a runnable Makie example that you have tried.

---

<div class="post-metadata">

**Author:** ![TheHedgehog2000](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@TheHedgehog2000](https://discourse.julialang.org/u/TheHedgehog2000)\
**Post date:** [May 1, 2024, 1:06am UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/3 "2024-05-01T01:06:31Z")

</div>

Sure – I just tried the first example provided in the rainclouds page ([https://docs.makie.org/stable/reference/plots/rainclouds/](https://docs.makie.org/stable/reference/plots/rainclouds/)), and played around with the keyword arguments. Below is the code. Here is an example of what I am going for (I would prefer vertical rather than horizontal, but that’s a minor detail): [Horizontal boxplot with observations — seaborn 0.13.2 documentation](https://seaborn.pydata.org/examples/horizontal_boxplot.html). Essentially, the difference is that the scatter points and boxes are centrally aligned (both to the x-ticks) in the latter case.

```julia
using CairoMakie

using Random
using Makie: rand_localized

####
#### Below is used for testing the plotting functionality.
####

function mockup_distribution(N)
    all_possible_labels = ["Single Mode", "Double Mode", "Random Exp", "Uniform"]
    category_type = rand(all_possible_labels)

    if category_type == "Single Mode"
        random_mean = rand_localized(0, 8)
        random_spread_coef = rand_localized(0.3, 1)
        data_points = random_spread_coef*randn(N) .+ random_mean

    elseif category_type == "Double Mode"
        random_mean = rand_localized(0, 8)
        random_spread_coef = rand_localized(0.3, 1)
        data_points = random_spread_coef*randn(Int(round(N/2.0))) .+ random_mean

        random_mean = rand_localized(0, 8)
        random_spread_coef = rand_localized(0.3, 1)
        data_points = vcat(data_points, random_spread_coef*randn(Int(round(N/2.0))) .+ random_mean)

    elseif category_type == "Random Exp"
        data_points = randexp(N)

    elseif category_type == "Uniform"
        min = rand_localized(0, 4)
        max = min + rand_localized(0.5, 4)
        data_points = [rand_localized(min, max) for _ in 1:N]

    else
        error("Unidentified category.")
    end

    return data_points
end

function mockup_categories_and_data_array(num_categories; N = 500)
    category_labels = String[]
    data_array = Float64[]

    for category_label in string.(('A':'Z')[1:min(num_categories, end)])
        data_points = mockup_distribution(N)

        append!(category_labels, fill(category_label, N))
        append!(data_array, data_points)
    end
    return category_labels, data_array
end

category_labels, data_array = mockup_categories_and_data_array(3)

colors = Makie.wong_colors()
rainclouds(category_labels, data_array;
    xlabel = "Categories of Distributions", ylabel = "Samples", title = "My Title",
    plot_boxplots = false, cloud_width=0.5, clouds=hist, hist_bins=50,
    color = colors[indexin(category_labels, unique(category_labels))])

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 1, 2024, 2:46am UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/4 "2024-05-01T02:46:24Z")

</div>

what about actually use [boxplot · Makie](https://docs.makie.org/stable/reference/plots/boxplot/) and scatter plot?

---

<div class="post-metadata">

**Author:** ![TheHedgehog2000](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@TheHedgehog2000](https://discourse.julialang.org/u/TheHedgehog2000)\
**Post date:** [May 1, 2024, 10:26am UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/5 "2024-05-01T10:26:21Z")

</div>

I would want the scatter plot to have random jitter ([Scatterplot Jittering](https://thomasleeper.com/Rcourse/Tutorials/jitter.html)). This is also what the rainclouds plot does. I might be able to write up something to generate the jitter correctly, I was only wondering if it is possible to do this using functions already written in the Makie package.

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [May 1, 2024, 3:32pm UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/6 "2024-05-01T15:32:21Z")

</div>

[GitHub - asinghvi17/SwarmMakie.jl: Beeswarm plots for Makie.jl!](https://github.com/asinghvi17/SwarmMakie.jl) should be able to give you a jittered scatter plot which you can plot separately from the boxplot - see e.g. [Uniform jitter](https://asinghvi17.github.io/SwarmMakie.jl/dev/algorithms#SwarmMakie.UniformJitter-algorithms)

---

<div class="post-metadata">

**Author:** ![TheHedgehog2000](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@TheHedgehog2000](https://discourse.julialang.org/u/TheHedgehog2000)\
**Post date:** [May 1, 2024, 3:44pm UTC](https://discourse.julialang.org/t/align-boxplot-and-scatter-points-in-a-rainclouds-plot-makie-jl/113675/7 "2024-05-01T15:44:57Z")

</div>

very cool, thanks!
