Annotations in groupedbar are broken?

I tried to add some numbers to each bar in groupedbar of StatsPlots and it always repeats the numbers of the first group. The error was reproduced in pgfplotsx, pyplot, and plotly as backends.

MWE:

x=["A","A","B","B"]
y=[1,2,3,4]
year=["2020","2021","2020","2021"]

groupedbar(x,y,group=year,bar_width=0.7,series_annotations=[1 2 3 4])

Even when the location of the annotations is not ideal (I think this is a known issue?), I’ve been trying the following workaround, but it still provides the numbers of the first group.

groupedbar!(x,y./2,group=year,bar_width=0.7,
alpha = 0.0,label="",series_annotations=[1 2 3 4])

The graph for the last code looks like the following
newplot

You should open an issue in StatsPlots.jl.

Meanwhile, you may check this wrapping function (not fully tested):

Code
function annotated_groupedbar(xx, yy, group; series_annotations="", legend=:top_right, bar_width=0.8, color=:blue, font_size=10)
    gp = groupedbar(xx, yy, group=group, series_annotations="", legend=legend, bar_width=bar_width)
    m = length(unique(group))       # number of items per group            
    n = length(unique(xx))          # number of groups
    xt = (1:n) .- 0.5               # plot x-coordinate of groups' centers
    dx = bar_width/m                # each group occupies bar_width units along x
    dy = diff([extrema(yy)...])[1]
    x2 = [xt[i] + (j - m/2 - 0.5)*dx for j in 1:m, i in 1:n][:]
    k = 1
    for i in 1:n, j in 1:m
        y0 = gp[1][2j][:y][i] + 0.04*dy
        if isfinite(y0)
            annotate!(x2[(i-1)*m + j], y0, text(series_annotations[k], :center, color, font_size))
            k += 1
        end
    end
    gp
end

For your example it produces:

using StatsPlots
x, y = ["A","A","B","B"], [1,2,3,4]
year = ["2020","2021","2020","2021"]
annotated_groupedbar(x, y, year; series_annotations=y, legend=:topleft, bar_width=0.7, color=:blue, font_size=10)

StatsPlot_groupedbar_custom_annotation_OP_ex

And for other more interesting groupings with gaps:

using StatsPlots
x, y = ["D","A","A","B","B","C","C","C","D"],  collect(1:9)
year = ["2020","2021","2022","2020","2022","2020","2021","2022","2021"]

annotated_groupedbar(x, y, year; series_annotations=y, legend=:topleft, bar_width=0.7, color=:blue, font_size=10)

annotated_groupedbar(year, y, x; series_annotations=y, legend=:topleft, bar_width=0.7, color=:blue, font_size=10)

StatsPlot_groupedbar_custom_annotation

StatsPlot_groupedbar_custom_annotation2

2 Likes