Legend customization issue

Hello everyone,

I am trying to create a plot using AlgebraOfGraphics separating the categorical values by linestyle and marker.

The generated legend is showing black boxes for both groups when I want it to show the marker and the linestyle of the categories. I cannot seem to find how to resolve it, so I would appreciate some help.

This is the code that I am using to generate the plot:

begin
  mean_profiles_data=data(mean_profiles_01group) *
  mapping(:Scheduled_time_hours => "Time after start of infusion (hours)",
  :mean => "Concentrations (μg/L)",
  :std, 
  linestyle=:"Population : 0.1 mg/kg" => nonnumeric,
  marker=:"Population : 0.1 mg/kg" => nonnumeric
  )*
  (visual(Errorbars;linewidth=5,color="#1f77b4") + visual(ScatterLines;markersize= 30,linewidth=6,color="#1f77b4") )
  mean_profiles_plt=draw(mean_profiles_data ;figure=(;fontsize=35,resolution=(1600,1600)),
  axis=(;xticks = (0:2:18),
  yticks = (0:50:250),
  limits = ((-1,19),(-10,200))),
  legend =(;position = :top),
  )
end

Here is an example plot i am generating to show the legend problem:

try to include group=:"Population : 0.1 mg/kg" => nonnumeric inside mapping?

Thank you Dr. Storopoli for your response but unfortunately the result is the same in my figure.

Hello @achamzas,

You could solve this issue by defining the ScatterLines plot and the Errorbars plot separately. To do this, we need to create a Figure and add an Axis to it:

fig = Figure(; fontsize=35, resolution=(1600, 1600))

ax = Axis(;
    xlabel="Time after start of infusion (hours)",
    ylabel="Concentrations (μg/L)",
    xticks=0:2:18,
    yticks=0:50:250,
    limits=(-1, 19, -10, 200)
)

Now you can define the plots (separating ScatterLines and Errorbars):

plt_scatterlines = data(mean_profiles_01group) *
                        mapping(
                            :Scheduled_time_hours,
                            :mean;
                            linestyle=:"Population : 0.1 mg/kg" => nonnumeric,
                            marker=:"Population : 0.1 mg/kg" => nonnumeric
                        ) *
                        (visual(Scatter) + visual(Lines))*visual(; markersize=30, linewidth=6, color="#1f77b4")

plt_errorbars = data(mean_profiles_01group) *
                        mapping(
                            :Scheduled_time_hours,
                            :mean,
                            :std
                        ) *
                        visual(Errorbars; linewidth=6, color="#1f77b4")

Notice that I used visual(Scatter) + visual(Lines) instead of visual(ScatterLines). Now you can use draw! to add the plots to the Axis:

legend_plot = draw!(ax, plt_scatterlines)
draw!(ax, plt_errorbars)

This will create a plot without a legend. You can add it using legend!:

legend!(
    fig[begin-1, 1],
    legend_plot;
    tellwidth=false,
    tellheight=true,
    nbanks=2
)

Please let me know if this works well for you.

Hi @jotas6 ,

Thank you very much for your reply, it did work!!! I only had to do a slight adjustment but it is what I wanted. The needed adjustment is to include the figure location in the Axis like this:

    ax = Axis(fig [1,1],
        xlabel="Time after start of infusion (hours)",
        ylabel="Concentrations (μg/L)",
        xticks=0:2:18,
        yticks=0:50:250,
        limits=(-1, 19, -10, 200)
    )

Happy to hear that! I forgot to add fig[1, 1] when creating the Axis, sorry about that

1 Like