How to add legend that overlays marker and line color

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.

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

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

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()

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

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

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

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

Example, with manual legends as @sdanisch just mentioned.

1 Like

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

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

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 ! :smiley:

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

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.

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