# AoG Barplot: How to match color of a single bar to theme?

When I generate grouped barplots in AoG, the colors of the bars are determined by the theme. However, barplots with a single group produce grey bars.
How can I set the bar colors to match the theme?

MWE:

using AlgebraOfGraphics, CairoMakie

df = DataFrame(x = [1,2,3], y = [1,2,3] .* 0.9, z = ["1", "2", "1"]);
set_aog_theme!();
plt1 = data(df) * mapping(:x, :y) * visual(BarPlot);
fig1 = draw(plt1)
plt2 = data(df) * mapping(:x, :y; color = :z) * visual(BarPlot);
fig2 = draw(plt2)

Output for fig1:

Output for fig2:

Oh, do you mean that you would like each bar to be the next color in the default color palette? If so, I guess we could map on :x then feed to verbatim to suppress the colorbar,

using CairoMakie, AlgebraOfGraphics

x = [1, 2, 3]
y = x .* 0.9
df = (; x, y)

plt = data(df) * mapping(:x, :y; color=:x => verbatim) * visual(BarPlot)
draw(plt)

or just set the color visually

plt = data(df) * mapping(:x, :y) * visual(BarPlot; color=x)
draw(plt)

Thanks, but my issue is with the plot without the color mapping. When I plot a single series, the bars are grey. That clashes with all the other bar graphs where the colors come from the theme.

So, Iām trying to replace the grey color in fig1 (in my example) with the first color from fig2.

Ah, do you mean like this equivalent in vanilla Makie with Cycled?

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

barplot!(ax, x, y; color=Cycled(1))

fig

It looks like there is currently an open issue that would allow this to work in AoG I think, for example with visual(BarPlot; color=Cycled(1)). In the meantime, could using the default color palette directly be a workaround?

set_aog_theme!()

plt = data(df) * mapping(:x, :y) * visual(BarPlot; color=Makie.wong_colors()[1])
draw(plt)

2 Likes

Thank you - that works.

And just to finish up: to match the single bar color with the first bar color of the theme:

thm = AlgebraOfGraphics.aog_theme();
barColors = first(thm.palette);
# and in the plot
visual(BarPlot; color = first(barColors))
1 Like