Inserting Custom Vertical/Horizontal Lines AlgebraOfGraphics.jl

Authors

Jose Storopoli

Juan Oneto

In this tutorial, we’ll cover how to insert custom vertical and horizontal lines in AoG.jl plots**.

To begin, like before, let’s load AoG.jl, data wrangling libraries and the DataFrame we’ve used previously:

using PharmaDatasets
using DataFramesMeta

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 now load AoG.jl along with CairoMakie.jl:

using CairoMakie
using AlgebraOfGraphics

1 đź“Ź Vertical and Horizontal Lines

AoG.jl does have specific plotting recipes for horizontal or vertical lines.

First, let’s create a simple plot with AoG.jl:

plt = data(df) * mapping(:AGE, :eGFR) * visual(Scatter);

As we can see, here is our plt drawn with AoG.jl:

draw(plt)

Now suppose you would want to insert custom horizontal lines between :eGFR values of 30 and 100 to draw attention to observations inside and outside this range.

We can do this with AoG.jl’s HLines and VLines:

plt_hlines = mapping([30, 100]) * visual(HLines);
draw(plt + plt_hlines)

Here we are passing the first and only positional argument to mapping since HLines only takes a single positional argument as a vector of the ys values that we want to draw a horizontal line at.

Notice that the procedure to have vertical lines is the same, but with VLines instead:

plt_vlines = mapping([30, 100]) * visual(VLines);
draw(plt + plt_vlines)

2 🎨 Customizing VLines and HLines

By default, VLines and HLines will use the colors from AoG.jl’s default theme palette (or any other custom palette you’ve set as the theme’s default).

Suppose you’ll want to customize the lines being drawn by VLines and HLines. You can use the same keyword arguments as Lines plotting type:

Tip

We use the plotting type in AoG.jl (Lines) but to get the attributes we call Makie.jl’s plotting function (lines).

help_attributes(lines)
Available attributes for `MakieCore.Lines` are: 

color colormap colorrange cycle depth_shift inspectable linestyle linewidth overdraw space ssao transparency visible

So let’s create the previous horizontal lines between :eGFR values of 30 and 100, but with linewidth set to 3, linestyle to :dash and color being :red with alpha 0.7:

draw(
    plt + (
        mapping([30, 100]) *
        visual(HLines; linewidth = 3, color = (:red, 0.5), linestyle = :dash)
    ),
)

3 đź’„ Filling regions with VSpan and HSpan

You can also fill vertical or horizontal regions of your plots with VSpan and HSpan, respectively.

The logic is similar as VLines and HLines. The first and second positional arguments inside the mapping are ys_low/xs_low and ys_high/xs_high, both being a vector of length-1 to specify the minimal or maximal lower bounds of either the horizontal or vertical regions.

Let’s fill the range that we’ve depicted in our last plot, between :eGFR values of 30 and 100:

plt_hspan = mapping([30], [100]) * visual(HSpan);
draw(plt + plt_hspan)

Of course, it also works for vertical spans:

plt_vspan = mapping([30], [100]) * visual(VSpan);
draw(plt + plt_vspan)

We can also customize HSpan and VSpan if we want to:

draw(plt + (mapping([30], [100]) * visual(HSpan; color = (:orange, 0.5))))

4 Conclusion

That’s it for our tutorial on custom horizontal and vertical lines in AoG.jl using visual([H|V]Lines).