Skip to content

Conversation

@jkrumbiegel
Copy link
Member

This PR adds the aggregate analysis which can aggregate one or more mapped columns grouped by one or more other columns. The aggregation functions can be chosen freely and the plotting function should just be picked as usual with visual, so it's a very flexible analysis layer. The reason for this is that it's usually a bit annoying having to add data wrangling just for some simple visualizations that should be done on the fly, where you are not interested in keeping the aggregated data around. This way you don't have to come up with a variable name for it, plus it works with all table inputs and not just the typical DataFrame.

For example, let's say we have some categories and associated measurements. We can plot these as a normal scatter:

using AlgebraOfGraphics
using CairoMakie
using Statistics

df = (
    cats = repeat(["low", "mid", "high"], 30),
    vals = repeat([1, 2, 3], 30) .+ randn.(),
)

base = data(df) * mapping(:cats, :vals)
scat = base * visual(Scatter)
draw(scat)
image

Let's say we want to show the median of each group. We can do this with aggregate. Every mapped column needs to be either a grouping column or an aggregated column. Grouping columns are denoted by a :.

med = base * aggregate(:, median) * visual(Scatter, markersize = 20, color = :red)
draw(scat + med)
image

Each column can only have one function applied, but this function may return multiple values per group, for example as a tuple. There can then be multiple functions that are applied on the result, each of which can be assigned to a different output mapping. This can be used, for example, to draw error bars or confidence intervals. Let's compute the 25th and 75th percentiles and draw the interval.

interval = base * aggregate(
    :,
    (x -> quantile(x, [0.25, 0.75])) => [
        first => 2,
        last => 3,
    ]
) * visual(Rangebars, linewidth = 3, color = :red)

draw(scat + med + interval)
image

With => 2 and => 3 we assign the first and second quantile to positional mappings 2 and 3 for Rangebars. If you don't specify a remapping, the initial mapping is kept, but there can only be one output assigned to a mapping.

In this case, it might look nice to apply a dodge, so both components can be discriminated better.

with_dodge = scat * mapping(dodge_x = direct("A")) +
    (med + interval) * mapping(dodge_x = direct("B"))

draw(with_dodge, scales(DodgeX = (; width = 0.2)))
image

Grouping by multiple mappings also works, for example to compute a heatmap by summing all values of a given group (the empty cells are combinations of x and y that don't exist by chance):

df = (;
    x = rand(1:5, 100),
    y = rand(1:5, 100),
    z = randn(100)
)
data(df) * mapping(:x, :y, :z) * aggregate(:, :, sum) *
    visual(Heatmap) |> draw
image

Additionally, the outputs can be renamed with the pair syntax, plus you can assign a scale id like with a normal mapping, because aggregate freely creates new mapped columns:

data(df) * mapping(:x, :y, :z) *
    aggregate(
        :,
        :,
        sum => "Sum of all z values" => scale(:sumcolor)
    ) *
    visual(Heatmap) |>
    draw(scales(sumcolor = (; colormap = :plasma)))
image

@jkrumbiegel jkrumbiegel changed the base branch from master to jk/0.12 November 6, 2025 12:11
@jkrumbiegel jkrumbiegel marked this pull request as ready for review November 6, 2025 13:46
@jkrumbiegel jkrumbiegel merged commit 62095da into jk/0.12 Nov 6, 2025
7 checks passed
@jkrumbiegel jkrumbiegel deleted the jk/aggregate branch November 6, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants