# Coloring boxplot groups in Makie

**URL:** <https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [June 7, 2021, 9:47am UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505 "2021-06-07T09:47:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 7, 2021, 9:47am UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/1 "2021-06-07T09:47:34Z")

</div>

Hi,

I’m discovering Makie and after reading the documentation, I don’t understand how to produce boxplots that have different colors. Here’s a MWE:

```julia
group = repeat([1,2],inner=500)
boxplot(rand(1:3,1000), randn(1000), dodge = group)

```

Which produces 6 boxplots, grouped two by two, as expected. I would just like color differently the right plot of each dodge but `color = group` does not work.

Thanks

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 7, 2021, 12:04pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/3 "2021-06-07T12:04:29Z")

</div>

It’s because I use Makie not StatsPlots. Sorry I should have specified it in the post.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [June 7, 2021, 12:17pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/4 "2021-06-07T12:17:13Z")

</div>

My bad you clearly said `Makie`. However, looking for examples I came across examples from `StatsPlots` and I then forgot you had specifically required `Makie`!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 7, 2021, 12:30pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/5 "2021-06-07T12:30:08Z")

</div>

> [@HenriDeh](#):
>
> ```julia
> group = repeat([1:50;50:-1:1],outer=2)
> boxplot(rand(1:3,1000), randn(1000), dodge = group)
> 
> ```

That gives me `DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 1000 and 200")` (Makie 0.13.11).

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 7, 2021, 12:38pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/6 "2021-06-07T12:38:02Z")

</div>

Ha sorry I pasted the wrong group. It’s supposed to be

`group = repeat([1,2], inner=500)`

Let me fix it

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [June 7, 2021, 1:22pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/7 "2021-06-07T13:22:32Z")

</div>

So, you could use [AlgebraOfGraphics](https://github.com/JuliaPlots/AlgebraOfGraphics.jl/) to generate the correct Makie boxplot as follows.

```julia
julia> using AlgebraOfGraphics, CairoMakie

julia> set_aog_theme!() # optional

julia> df = (x = rand(1:3, 1000), y = rand(1000), group = rand(1:2, 1000));

julia> plt = data(df) * mapping(:x, :y, color = :group => nonnumeric, dodge = :group => nonnumeric) * visual(BoxPlot);

julia> draw(plt)

```

 ![boxplot](https://global.discourse-cdn.com/julialang/original/3X/3/3/33ec096fabd516b25b2d6b85161e13dd9697e0e6.png)

Doing this plot directly with Makie is a bit clumsy at the moment, but I’m planning to make a PR to support passing a vector of colors (see examples with [violin](https://makie.juliaplots.org/dev/plotting_functions/violin.html), where this is already possible).

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [June 7, 2021, 1:34pm UTC](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505/8 "2021-06-07T13:34:31Z")

</div>

Ha okay great. I did not know Algebra of Graphics. Thanks!
