How to group/color lines per ID in Makie?

Hi all. I am using CairoMakie.jl to plot a figure for longitudinal theophylline concentration data in 12 subjects simultaneously. The code I am using is

Theoph = dataset("datasets", "Theoph")
f = Figure()
Axis(f[1,1])
CairoMakie.lines!(Theoph.Time, Theoph.Conc)
CairoMakie.scatter!(Theoph.Time, Theoph.Conc)
f

The result looks like this.

I need to group or color the lines per subject to avoid connecting all lines together! Any ideas?!

I can be more specific if you tell me more about the format of Theoph, but I would recommend adding the AlgebraOfGraphics package to make plotting datasets easier. Something like:

using AlgebraOfGraphics, CairoMakie
draw(data(Theoph) * mapping(:Time, :Conc, color=:Subject) * visual(ScatterLines))
1 Like

Second the advice to use AlgebraOfGraphics. It saves you exactly from the annoyance of writing out the grouping logic for these kinds of plots over and over again.

1 Like

Thanks a lot for the suggestion. I will try it out. Does this mean that CairoMakie.jl itself does not have a means to handle this?

worked beutifully :slight_smile:

Makie is more low level, grouping a dataframe and automatically creating a legend is currently outside of its scope. But you can create more flexible solutions than with a framework like AlgebraOfGraphics. And you can simply combine both, AlgebraOfGraphics creates normal Makie plots that you can modify further.

1 Like

Looks like it would be nice for AOG to have sorting on categorical axis labels

Yeah the dataset Subject column levels were messed up. When I reassign levels I get the correct order.

Yea, for the sorting bit I think there is sorter:

labels = df.Subject|> unique
mapping(:Time, :Conc; color=:Subject => sorter(labels))

so you can specify whatever order you want

2 Likes