# Annotations in groupedbar are broken?

**URL:** <https://discourse.julialang.org/t/annotations-in-groupedbar-are-broken/74418>\
**Category:** Visualization\
**Tags:** plots, statsplots\
**Created:** [January 11, 2022, 6:35pm UTC](https://discourse.julialang.org/t/annotations-in-groupedbar-are-broken/74418 "2022-01-11T18:35:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [January 11, 2022, 6:35pm UTC](https://discourse.julialang.org/t/annotations-in-groupedbar-are-broken/74418/1 "2022-01-11T18:35:08Z")

</div>

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:

```julia
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.

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/3/d/3dc40a86ded4c68d24ccd613d2bae7b3679a2164.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 15, 2022, 2:56pm UTC](https://discourse.julialang.org/t/annotations-in-groupedbar-are-broken/74418/2 "2022-01-15T14:56:15Z")

</div>

You should open an issue in StatsPlots.jl.

Meanwhile, you may check this wrapping function (_not fully tested_):

> **Code**
>
> ```julia
> 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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3875e9ddfc3a8395fdb3768ded47b22303f1d4f.png)

And for other more interesting groupings with gaps:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/5/5/55f9135e5d55b252ec02d24a8be7b68945b59a8f.png)

![StatsPlot_groupedbar_custom_annotation2](https://global.discourse-cdn.com/julialang/original/3X/6/a/6aea92c8fbd43920f3abdb645149cad293ed4d73.png)
