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:
Finally, load AlgebraOfGraphics.jl and CairoMakie.jl:
usingAlgebraOfGraphicsusingCairoMakie
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:
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: