# Styling AlgebraOfGraphics boxplots

**URL:** <https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335>\
**Category:** Visualization\
**Tags:** makie, algebraofgraphics\
**Created:** [July 26, 2021, 10:38pm UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335 "2021-07-26T22:38:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 26, 2021, 10:38pm UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/1 "2021-07-26T22:38:01Z")

</div>

I’m amazed how easy it is to make complex and good-looking figures using AlgebraOfGraphics - it is still a little opaque as to how to use custom styles in them though.

 ![canberratemp](https://global.discourse-cdn.com/julialang/original/3X/0/7/07cc025514bc56baadf272dff8dbb48ebb6a46fb.png)

I’d like to be able to change the colors in this temperature distribution plot to be say, red and blue. Examples suggest that you can do so for many types of AlgebraOfGraphics plots by calling e.g.

```julia
Visual(Scatter; color=:red)

```

but the equivalent doesn’t seem to work for boxplots

---

<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:** [July 27, 2021, 1:10am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/2 "2021-07-27T01:10:01Z")

</div>

Doesn’t something like this work?

```
boxplot(dataframe, :x, :y, color = :red)

```

You have not posted your code, so not sure without a computer what your commands are, but I remember doing it for violin.

Edit: that’s more like the `StatsPlots` syntax, sorry for the noise.

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 27, 2021, 1:15am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/3 "2021-07-27T01:15:32Z")

</div>

The relevant parts of the code (where mintemperature and maxtemperature are dataframes) are

```julia

temperature = innerjoin(mintemperature, maxtemperature, on = [:"Bureau of Meteorology station number", :Year, :Month, :Day], makeunique=true)

tstartstop = @chain temperature begin
    groupby(Symbol("Bureau of Meteorology station number"))
    combine(:Year => minimum, :Year=>maximum)
	transform!([:Year_minimum, :Year_maximum] => ((x,y) -> y.-x) => :"Year_total")
end

	axis = (width = 1000, height = 750)
	sr = renamer(70349=>"Mount Ginini. [$(tstartstop[tstartstop."Bureau of Meteorology station number".==70349, "Year_total"][1]) Years]", 
		        70351=>"Canberra Airport [$(tstartstop[tstartstop."Bureau of Meteorology station number".==70351, "Year_total"][1]) Years]")
	
	pcr = renamer("IDCJAC0010"=>"Daily Maximum", "IDCJAC0011"=>"Daily Minimum")
	
	tr2 = renamer(1=>"Jan",
				 2=>"Feb",
				 3=>"Mar",
				 4=>"Apr",
		         5=>"May",
		         6=>"Jun",
		         7=>"Jul",
		         8=>"Aug",
		         9=>"Sep",
		         10=>"Oct",
		         11=>"Nov",
		         12=>"Dec")
	
	tm = (mapping(:Month=>tr2, 
		 	    :"Minimum temperature (Degree C)"=>"Temperature (Degree C)",
		        color=:"Product code"=>pcr, dodge=:"Product code"=>pcr,
		        col=:"Bureau of Meteorology station number"=>sr)+
	      mapping(:Month=>tr2, 
		 	    :"Maximum temperature (Degree C)"=>"Temperature (Degree C)",
		        color=:"Product code_1"=>pcr, dodge=:"Product code"=>pcr,
		        col=:"Bureau of Meteorology station number"=>sr))
		
	tplt = (data(dropmissing(temperature)) * tm * visual(BoxPlot))
	
	tfig = draw(tplt; axis)

```

I’ve tried

```julia
visual(BoxPlot; color=[:red, :blue])

```

and

```julia
visual(BoxPlot; color=[[:red, :blue],[:red,:blue]])

```

both are ignored without throwing an error

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 27, 2021, 5:46am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/4 "2021-07-27T05:46:01Z")

</div>

I can’t find it right now but I think you want to change the palette where the categorical colors are defined in the first place, not pass colors directly. I think it’s something like `draw(aog, palettes = (color = [:red, :green],))`

---

<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:** [July 27, 2021, 6:08am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/5 "2021-07-27T06:08:54Z")

</div>

If you want to change the default colors, as Jules says, you should define your own theme and use it as the default. I couldn’t find any docs or blogs examples of how to do that. However, you can follow the way that function `aog_theme` is defined (see link below). Make your own `my_aog_theme` and use that. It will be fiddly the first time around, but afterwards you can use the theme with all your plots. (full disclosure: I haven’t used `AlgebraOfGraphics` yet, but it is inspired by the `ggplot2` philosophy, which I use, and where I have defined my own styles: it’s well worth the effort).

[https://raw.githubusercontent.com/JuliaPlots/AlgebraOfGraphics.jl/5b7f3e767dcacbecfeb424f8a633578271ed2747/src/theme.jl](https://raw.githubusercontent.com/JuliaPlots/AlgebraOfGraphics.jl/5b7f3e767dcacbecfeb424f8a633578271ed2747/src/theme.jl)

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 27, 2021, 6:23am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/6 "2021-07-27T06:23:14Z")

</div>

I guess if I was going to actually make many of these sorts of plots it would certainly be worth it (along with packaging up things like the month-month string renamer); it is a bit of overkill if you are doing a one-off like this, especially when the default style is (in my opinion) really nice!

---

<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:** [July 27, 2021, 7:14am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/7 "2021-07-27T07:14:54Z")

</div>

There’s probably an easy way to pass your own palette, as Jules suggested. You may want to look into that. Maybe package [ColorSchemes](https://docs.juliaplots.org/latest/generated/colorschemes/) works out of the box (it works great with simple `Plots` situations I’ve tried).

Here’s an issue I’ve made, hopefully some experts will weigh in: [https://github.com/JuliaPlots/AlgebraOfGraphics.jl/issues/245](https://github.com/JuliaPlots/AlgebraOfGraphics.jl/issues/245)

Just for the record, here’s a related discussion: [Coloring boxplot groups in Makie](https://discourse.julialang.org/t/coloring-boxplot-groups-in-makie/62505)

---

<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:** [July 27, 2021, 7:55am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/8 "2021-07-27T07:55:23Z")

</div>

Finally got it to work. **EDIT** : Code much simplified after seeing lazarus’s answer below (I was missing the comma needed for a tuple of size 1, which led me to embark on some unnecessarily complicated workaround). To sum up, if you want to set a theme so all your plots get the same color treatment, follow the steps I outline immediately below. If you want to change the color for a single plot, use the `palettes` (with an s at the end of palettes) option inside `draw`, as shown by lazarusA in his answer further down.

```
using AlgebraOfGraphics, CairoMakie
using Colors # for the palette
set_aog_theme!()
update_theme!(
    Theme(
        palette = (color = ["#1f78b4", "#ff7f00", "#029e73"],) # DON'T FORGET THE COMMA HERE!
    )
)

df = (x=rand(["a", "b", "c"], 100), y=rand(100))
plt = data(df) * mapping(:x, :y, color = :x) * visual(BoxPlot)
draw(plt)

```

 ![plot](https://global.discourse-cdn.com/julialang/original/3X/0/e/0ef0d71309040fa22770d0845db41e8dbb75808e.png)

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [July 27, 2021, 7:56am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/9 "2021-07-27T07:56:36Z")

</div>

I do follow @jules suggestion [here.](https://lazarusa.github.io/BeautifulMakie/ScattersLines/penguinsAoGsides/)

```julia
aesHist = data(penguins) * mapping(:bill_length_mm => "bill length mm",
        color = :species, stack = :species) * AlgebraOfGraphics.histogram(;bins = 20)

    estilo = (color=["#FC7808", "#8C00EC", "#107A78"],
                marker=[:circle, :utriangle, :rect])
    # the actual plot with AoG and Makie
    fig = Figure(; resolution = (700, 700))
    axH = draw!(fig[1,1], aesHist, palettes = estilo)

```

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [July 27, 2021, 8:07am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/10 "2021-07-27T08:07:43Z")

</div>

this also works for me:

```julia
using AlgebraOfGraphics, CairoMakie
using Colors
colors = ["#FC7808", "#8C00EC", "#107A78"]
style = (color= colors, )
df = (x=rand(["a", "b", "c"], 100), y=rand(100))
plt = data(df) * mapping(:x, :y, color = :x) * visual(BoxPlot)
draw(plt, palettes = style)

```

---

<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:** [July 27, 2021, 8:10am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/11 "2021-07-27T08:10:31Z")

</div>

> [@lazarusA](#):
>
> `style = (color= colors, )`

Wow, great job lazarusA!

That comma was essential, I was going crazy without it.

That comma, if I understand, is there because it is a tuple of size 1.

---

<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:** [July 27, 2021, 8:51am UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/12 "2021-07-27T08:51:43Z")

</div>

> [@ptoche](#):
>
> That comma, if I understand, is there because it is a tuple of size 1.

Yep. As an alternative I like to write e.g. `style = (; color=colors)` instead of a dangling comma. I find the leading `;` less easy to forget. It also has more general use, for example in the following:

```julia
a = [1,2,3]
b = ["xx", "xy", "yy"]
df = DataFrame(; x, y) # ; required so x,y are treated as keyword arguments

```

You could actually use this feature in your case with

```julia
using AlgebraOfGraphics, CairoMakie
color = ["#FC7808", "#8C00EC", "#107A78"]
df = (x=rand(["a", "b", "c"], 100), y=rand(100))
plt = data(df) * mapping(:x, :y, color=:x) * visual(BoxPlot)
draw(plt, palettes=(; color))

```

---

<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:** [July 27, 2021, 10:30pm UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/13 "2021-07-27T22:30:26Z")

</div>

> [@sijo](#):
>
> `draw(plt, palettes=(; color))`

I agree the semi-colon is easier to see! 😀

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 27, 2021, 10:30pm UTC](https://discourse.julialang.org/t/styling-algebraofgraphics-boxplots/65335/14 "2021-07-27T22:30:53Z")

</div>

Thanks everyone for the discussion!

 ![index](https://global.discourse-cdn.com/julialang/original/3X/a/7/a798fb2cdef2e77fef2b518e25758391ce3dea4c.png)
