Placing text labels in AlgebraOfGraphics.jl

Author

Patrick Kofod Mogensen

In this tutorial, we will cover how to place text annotation in AlgebraOfGraphics.jl. Annotation can be useful when producing plots of covariate information, weighted residuals or other plots where it may be useful to visually document outlier information.

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

Finally, load AlgebraOfGraphics.jl and CairoMakie.jl:

using AlgebraOfGraphics
using CairoMakie

1 An Age Against Weight Scatter Plot

To illustrate how to use text annotation in AlgebraOfGraphics let us make a scatter plot with the AGE variable on the first axis and WEIGHT on the second axis.

spec1 = data(df) * mapping(:AGE, :WEIGHT) * visual(Scatter; label = "Data")
draw(
    spec1;
    axis = (xlabel = "Age (years)", ylabel = "Weight (kg)"),
    legend = (position = :top, titleposition = :left),
)

This looks pretty good, but suppose we would want to dive deeper into the data and look at some of the outliers. Then we could print the IDs in the plot. If the data is much more dense than this it may not be feasible, but in this example it looks like it would work.

To overlay IDs as text we can use the Makie.Text visual with options given as keyword arguments to visual as usual.

df.ID .= string.(df.ID)
spec2 =
    spec1 +
    data(df) *
    mapping(:AGE, :WEIGHT, text = :ID => verbatim) *
    visual(Makie.Text; color = :black, font = :bold, align = (:left, :bottom))
draw(
    spec2;
    axis = (xlabel = "Age (years)", ylabel = "Weight (kg)"),
    legend = (position = :top, titleposition = :left),
)

From this, we can see that subject “12” may be of particular interest and maybe even the cluster of subjects with Weight > 90 kg.

We can create a new plot with only the subjects in the cluster with weight above 90 kg having their IDs printed. By passing a reduced DataFrame for the part of the plot specification that controls the Makie.Text visual, we can get the desired plot.

df.ID .= string.(df.ID)
spec2 =
    spec1 +
    data(filter(x -> x.WEIGHT > 90, df)) *
    mapping(:AGE, :WEIGHT, text = :ID => verbatim) *
    visual(Makie.Text; color = :black, font = :bold, align = (:left, :bottom))
draw(
    spec2;
    axis = (xlabel = "Age (years)", ylabel = "Weight (kg)"),
    legend = (position = :top, titleposition = :left),
)

2 Conclusion

That’s it for our short tutorial on using text annotation AlgebraOfGraphics.jl plots. Hopefully, this tutorial has inspired you to create your own annotated plots in your own use cases and workflows.a