Adjusting legend linewidth, markersize, and alpha with AlgebraOfGraphics.jl

Author

Patrick Kofod Mogensen

This feature requires AlgebraOfGraphics v0.8.12. If you have Pumas v2.6.0 or above the correct version of AlgebraOfGraphics is available to you.

In this tutorial, we will cover how to adjust the legend markersize, linewidth and alpha in AlgebraOfGraphics.jl plots. Sometimes, we want a thin line, a small marker, or very low alpha to clearly communicate our data. This can make the legend appear unintelligible. In this tutorial, we will show you how to fix that by adjusting the legend keyword in visual to make the legend content differ from the plot itself.

To begin, like in other AlgebraOfGraphics tutorials, let’s load AlgebraOfGraphics.jl, data wrangling libraries and the DataFrame we’ve used previously:

using PharmaDatasets
using DataFrames

df = dataset("demographics_1")
first(df, 5)
5×6 DataFrame
Row ID AGE WEIGHT SCR ISMALE eGFR
Int64 Float64 Float64 Float64 Int64 Float64
1 1 34.823 38.212 1.1129 0 42.635
2 2 32.765 74.838 0.8846 1 126.0
3 3 35.974 37.303 1.1004 1 48.981
4 4 38.206 32.969 1.1972 1 38.934
5 5 33.559 47.139 1.5924 0 37.198

and let us add a group for SEX

df[!, :SEX] .= @. ifelse(df.ISMALE == 1, "MALE", "FEMALE")
100-element Vector{String}:
 "FEMALE"
 "MALE"
 "MALE"
 "MALE"
 "FEMALE"
 "FEMALE"
 "FEMALE"
 "FEMALE"
 "FEMALE"
 "MALE"
 ⋮
 "MALE"
 "FEMALE"
 "FEMALE"
 "MALE"
 "MALE"
 "MALE"
 "FEMALE"
 "MALE"
 "FEMALE"

Finally, load AlgebraOfGraphics.jl and CairoMakie.jl:

using AlgebraOfGraphics
using CairoMakie

1 Adjusting scatter plot legend markersize

Sometimes we want to increase the markersize in the legend to make it more obvious. The standard plot looks as follows.

spec = data(df) * mapping(:AGE, :WEIGHT, color = :SEX) * visual(Scatter)

draw(spec)

We see the legend and the markers match. To change the size of the marker we need to set the markersize in the visual. Let us make them large:

spec = data(df) * mapping(:AGE, :WEIGHT, color = :SEX) * visual(Scatter; markersize = 25)

draw(spec)

or small:

spec = data(df) * mapping(:AGE, :WEIGHT, color = :SEX) * visual(Scatter; markersize = 4)

draw(spec)

The markers can become obnoxiously large or so small we cannot see them in the legend even if we think it’s appropriate for the plot itself. We can change the marker in the legend by supplying the legend keyword to the visual:

spec =
    data(df) *
    mapping(:AGE, :WEIGHT, color = :SEX) *
    visual(Scatter; markersize = 4, legend = (; markersize = 15))

draw(spec)

Notice, that setting the actual markersize of the plot is done by adjusting the markersize keyword to visual directly. The marker itself is controlled through the keyword markersize to visual.

2 Adjusting line plot legend linewidth

If we have a Lines plot we may want to change the linewidth. Without any keywords set we get the following plot:

spec = data(df) * mapping(:AGE, :WEIGHT, color = :SEX) * visual(Lines)

draw(spec)

With the linewidth keyword to visual set to 1 we get thinner lines in the plot and in the legend.

spec = data(df) * mapping(:AGE, :WEIGHT, color = :SEX) * visual(Lines; linewidth = 1)

draw(spec)

If we want to make the lines easier to see in the legend we can again use the legend keyword in the visual:

spec =
    data(df) *
    mapping(:AGE, :WEIGHT, color = :SEX) *
    visual(Lines; linewidth = 1, legend = (; linewidth = 5))

draw(spec)

3 Conclusion

That’s it for our tutorial on adjusting legend entries’ visual appearance in AlgebraOfGraphics.jl using the legend keyword to visual.