# Create legend for dodge boxplot in Makie

**URL:** https://discourse.julialang.org/t/create-legend-for-dodge-boxplot-in-makie/97824
**Category:** Visualization
**Tags:** makie, cairomakie-plotting
**Created:** [April 23, 2023, 5:52pm UTC](https://discourse.julialang.org/t/create-legend-for-dodge-boxplot-in-makie/97824 "2023-04-23T17:52:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vembha](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vembha](https://discourse.julialang.org/u/vembha)
#### Post date: [April 23, 2023, 5:52pm UTC](https://discourse.julialang.org/t/create-legend-for-dodge-boxplot-in-makie/97824/1 "2023-04-23T17:52:29Z")

</div>

Hello,

I am trying to get a legend for a box plot created in Makie. For the example provided in the website [Page Redirection](https://docs.makie.org/stable/examples/plotting_functions/boxplot/) (attached is the box plot), how to get a legend inside the figure panel? Like in a scatter plot, I expect two legend items - one for each color.

![చిత్రము](https://global.discourse-cdn.com/julialang/original/3X/0/f/0f9488f898f6f21fb25e384f0152cfdabe7bdf22.png)

How to do it?

---

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [April 25, 2023, 10:42am UTC](https://discourse.julialang.org/t/create-legend-for-dodge-boxplot-in-makie/97824/2 "2023-04-25T10:42:36Z")

</div>

Hi, I am not sure if there is an easier way, but I am currently handling such situations as follows

```julia
begin
	xs = rand(1:3, 1000)
	ys = randn(1000)
	dodge = rand(1:2, 1000)
	
	f,a,p = boxplot(xs, ys, dodge = dodge, show_notch = true, color = dodge)

	# get current colormap
	cmap = getproperty(ColorSchemes, p.colormap[])
	# get colors used (length=2 in this case)
	ccolors = cmap[range(start=0.0, stop=1.0; length=length(unique(dodge)))]

	# build the marker elements
	elems = [[MarkerElement(color = col, marker=:circle, markersize = 15,
          strokecolor = :black)] for col in ccolors]
	
	axislegend(a, elems, ["First text", "Second text"]; position=:rt)
    
	f
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/9/493aec9d741aa53fecd0e406896bf66d0af2620c.png)

---

<div class="post-metadata">

### Author: ![vembha](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vembha](https://discourse.julialang.org/u/vembha)
#### Post date: [April 25, 2023, 11:05am UTC](https://discourse.julialang.org/t/create-legend-for-dodge-boxplot-in-makie/97824/3 "2023-04-25T11:05:21Z")

</div>

It’s working! Thank you. 🙂
