How to label multiple histograms in AlgebraOfGraphics

I have a dataset where each column is a list of occurrences. I would like to plot three columns as three histograms within one figure, but I cannot wrap my head around the AlgebraOfGraphics idioms.

Within Makie.jl, I would do

fig = Figure()
ax = Axis(fig[1,1])

hist!(ax, df[:pf],  label = "Plasma frame")
hist!(ax, df[:sf],  label = "Shock frame")
hist!(ax, df[:ISM], label = "ISM frame")

axislegend(ax)

fig

where df is my data source and each of df[:pf], df[:sf], and df[:ISM] is a Vector.

With AlgebraOfGraphics.jl, I’ve tried

using AlgebraOfGraphics
import AlgebraOfGraphics as AoG

data = AoG.data(df)
m = AoG.mapping(:pf  => "Plasma frame") +
    AoG.mapping(:sf  => "Shock frame")  +
    AoG.mapping(:ISM => "ISM frame")
hist_layer = AoG.histogram()

layer = data * m * hist_layer

The issue I’m having is with labeling each histogram. I know the => Pair syntax labels the axes, but I couldn’t figure out how to add data series labels from the AoG documentation.

The plain label is done via visual(label = "label") but then you have to do the colors yourself. Easier would be to pass your three columns as a vector and then use dims(1) for color, check this tutorial for reference Intro to AoG - V - Alternative input data formats | AlgebraOfGraphics

Hi! Thank you so much! This almost gets to the result I want, except the legend is labeled with CartesianIndex(i,). It’s not picking up that the labels for each series should be the column name

You can do that via renamer like in the tutorial. The dimensions are not automatically named by input columns, because they are not necessarily defined like that. But maybe in the future that could be built in as a convenient shortcut or something

1 Like

Oh yeah the lack of automatic naming was an annoyance I had that I never mentioned.

It would be really cool if AlgebraOfGraphics.jl leveraged table-level metadata, particularly the label field.

You had mentioned it :slight_smile: Make the `color = dims(1)` correspond to the `mapping` for wide data · Issue #474 · MakieOrg/AlgebraOfGraphics.jl · GitHub

I still haven’t looked into how that might be implemented, maybe I can hook into the labels somehow

1 Like