# How to add legend that overlays marker and line color

**URL:** <https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [May 21, 2021, 10:03am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571 "2021-05-21T10:03:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vjd/32/2644_2.png) [@vjd](https://discourse.julialang.org/u/vjd)\
**Post date:** [May 21, 2021, 10:03am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/1 "2021-05-21T10:03:04Z")

</div>

is it possible to have a the legend for marker and line overlay on top each other if they follow the same color as in the code and figure below.

> I am still not sure exactly on the thinking behind creating the legend from a _mapped_ variable in the dataset. If some one can explain in more detail, I would appreciate it.

```julia
dd = DataFrame(id = [1,1,1,1,1,2,2,2,2,2],
               time = [10,20, 30, 40, 50, 10,20, 30, 40, 50],
               dv = [5.0, 4.0, 3.0, 2.0, 1.0,10.0, 9.0, 8.0, 7.0, 6.0],
               dv2 = [5.0, 4.0, 3.0, 2.0, missing,10.0, 9.0, 8.0, 7.0, missing])
dd[!, :dv2] = coalesce.(dd[!, :dv2], NaN)
palette = ColorSchemes.tab10.colors
color_indices = groupindices(groupby(dd, :id))
fig = Figure(resolution = (1000, 700))
ax1 = fig[1, 1] = Axis(fig, 
  title = "",
  xlabel="Time (min)", 
  ylabel="Concentration (μg/L)")               
scatter!(ax1, 
  dd[!,:time], dd[!,:dv2], 
  color=palette[color_indices])
lines!(ax1, 
  dd[!,:time], dd[!,:dv2],
  color=palette[color_indices])  
fig

```

 ![2021-05-21 at 5.58 AM](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f588987c4c0acb907d0c98bda952f7fbc3b19e2.png)

basically I want he the legend to be like this (generated by ggplot2 below)

```julia
dd = data.frame(id = c(1,1,1,1,1,2,2,2,2,2),
               time = c(10,20, 30, 40, 50, 10,20, 30, 40, 50),
               dv = c(5.0, 4.0, 3.0, 2.0, 1.0,10.0, 9.0, 8.0, 7.0, 6.0),
               dv2 = c(5.0, 4.0, 3.0, 2.0, NA,10.0, 9.0, 8.0, 7.0, NA))
dd %>% 
  ggplot(aes(x = time, y = dv2,color=factor(id)))+
  geom_point() + geom_line()

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/1/816753e0e5e7103e3c474c9b2867bd852e061e03.png)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 21, 2021, 10:09am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/2 "2021-05-21T10:09:01Z")

</div>

If you use `scatterlines` it should work like that 😉  
Or [https://github.com/JuliaPlots/AlgebraOfGraphics.jl](https://github.com/JuliaPlots/AlgebraOfGraphics.jl), which gives you a whole algebra to combine plot types like that…

---

<div class="post-metadata">

**Author:** ![vjd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vjd/32/2644_2.png) [@vjd](https://discourse.julialang.org/u/vjd)\
**Post date:** [May 21, 2021, 10:13am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/3 "2021-05-21T10:13:21Z")

</div>

Thanks. But what if I have two different arrays, one for lines and one for scatter, both coming from the same if and I want that behavior

---

<div class="post-metadata">

**Author:** ![vjd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vjd/32/2644_2.png) [@vjd](https://discourse.julialang.org/u/vjd)\
**Post date:** [May 21, 2021, 10:22am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/4 "2021-05-21T10:22:27Z")

</div>

`scatterlines` with the code above only colors the lines and not the markers though

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 21, 2021, 2:36pm UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/5 "2021-05-21T14:36:24Z")

</div>

Yeah you need to set `markercolor` separately which is a bit annoying…

> But what if I have two different arrays, one for lines and one for scatter

I guess then you may need to use AOG… Or create the legend manually:

[https://makie.juliaplots.org/stable/makielayout/legend.html#Creating-A-Legend-From-Elements](https://makie.juliaplots.org/stable/makielayout/legend.html#Creating-A-Legend-From-Elements)

---

<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:** [May 21, 2021, 3:40pm UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/6 "2021-05-21T15:40:56Z")

</div>

Example, with manual legends as @sdanisch just mentioned.

> **[Jupyter Notebook Viewer](https://nbviewer.org/github/lazarusA/MakieNotebooks/blob/main/FigLineScatter2.ipynb)**
>
> Check out this Jupyter notebook!

---

<div class="post-metadata">

**Author:** ![vjd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vjd/32/2644_2.png) [@vjd](https://discourse.julialang.org/u/vjd)\
**Post date:** [May 21, 2021, 3:47pm UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/7 "2021-05-21T15:47:30Z")

</div>

Thanks, saw that. Could you help me in the context of a data frame and the example above please.

---

<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:** [May 21, 2021, 5:12pm UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/8 "2021-05-21T17:12:49Z")

</div>

the problem with your previous approach is that you are creating two lines or scatter classes at the same time (NaNs in between), and as I can see currently its not possible to get references for both cases… I was expecting to be able to do something like

```julia
slegs = scatter!(ax1, 
  dd[!,:time], dd[!,:dv2], 
  color=palette[color_indices])
lineslegs = lines!(ax1, 
  dd[!,:time], dd[!,:dv2], 
  color=palette[color_indices])

```

and then from there retrieve the references for the labels.  
`leg = Legend(fig, [[lineslegs[1], slegs[1]], [lineslegs[2], slegs[2]]], ["1", "2"]`

and just do

`fig[1,2] = leg`  
This doesn’t work ! 😃

otherwise it can be done, but you need to plot each line and scatter independently, as in the example in the link.

BTW. If we wait a little bit, this is pretty easy with AlgebraOfGraphics  
[http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/gallery/#Legend-merging](http://juliaplots.org/AlgebraOfGraphics.jl/dev/generated/gallery/#Legend-merging)

---

<div class="post-metadata">

**Author:** ![vjd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vjd/32/2644_2.png) [@vjd](https://discourse.julialang.org/u/vjd)\
**Post date:** [May 21, 2021, 6:48pm UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/9 "2021-05-21T18:48:01Z")

</div>

Thanks. Finally, this code below worked for others who are following this thread. Still not ideal as what I was looking for (overlay of marker and line in legend), but will be good for now.

```julia
palette = ColorSchemes.tab10.colors
fig = Figure(resolution = (1000, 700))
ax1 = fig[1, 1] = Axis(fig,
           title = "",
           xlabel="Time (min)",
           ylabel="Concentration (μg/L)",
           yscale = log10,
           yminorticksvisible = true, yminorgridvisible = true,
                 yminorticks = IntervalsBetween(8))
#colors = [:red, :blue]
for (i, df) in enumerate(groupby(dd, :id; sort = true))
           scatter!(ax1, df[!, :time], df[!, :dv]; label = "ID: $(df[1, :id])", color = palette[i])
           lines!(ax1, df[!, :time], df[!, :dv]; label = "ID: $(df[1, :id])", color = palette[i])
end
Legend(fig[1, 2], ax1)
fig

```

 ![2021-05-21 at 2.47 PM](https://global.discourse-cdn.com/julialang/original/3X/8/1/818f62cc80c7cba845d85e7038a6eb33b16b838e.png)

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [April 28, 2025, 9:09am UTC](https://discourse.julialang.org/t/how-to-add-legend-that-overlays-marker-and-line-color/61571/10 "2025-04-28T09:09:08Z")

</div>

I guess this was the initial query. Maybe it will be helpful for future reference 🙂

```julia
dd = DataFrame(id = [1,1,1,1,1,2,2,2,2,2],
               time = [10,20, 30, 40, 50, 10,20, 30, 40, 50],
               dv = [5.0, 4.0, 3.0, 2.0, 1.0,10.0, 9.0, 8.0, 7.0, 6.0],
               dv2 = [5.0, 4.0, 3.0, 2.0, missing,10.0, 9.0, 8.0, 7.0, missing])
dd[!, :dv2] = coalesce.(dd[!, :dv2], NaN)

plot( dd[1:5,:time], dd[1:5,:dv2], marker = :circle, color = :red)
plot!( dd[6:end,:time], dd[6:end,:dv2], marker = :circle, color = :blue)

```

**Main trick is “marker = :circle”**

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/d/fdab08c9d63168c939dc2b94d7dab0c856afec0c.png)
