# Grouped bar plots, with different number of bars in each group

**URL:** <https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517>\
**Category:** Visualization\
**Tags:** bar\
**Created:** [October 10, 2022, 10:07am UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517 "2022-10-10T10:07:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 10, 2022, 10:07am UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/1 "2022-10-10T10:07:16Z")

</div>

I have a few data sets:

```julia
d1 = [1,3,4,3,2]
d2 = [2]
d3 = [3]
d4 = [5,3]

```

I want to create a bar plot, with one group for each data set, containing 4/1/1/2 bars respectively. Is this possible? I found `groupedbar`, but that only seems to work if each group have the same number of bars.

I guess this would be possible by customizing the position and width of each individual of the 9 bars. But I want to make quite a few such plots, typically with more bars, so this would become difficult.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [October 10, 2022, 11:03am UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/2 "2022-10-10T11:03:43Z")

</div>

Attempt with Makie :

 ![toto](https://global.discourse-cdn.com/julialang/original/3X/1/5/1530684185e277f140809f8b7c49cd41785f1b95.png)

```julia
using CairoMakie

function test_bar()
    d1 = [1,3,4,3,2]
    d2 = [2]
    d3 = [3]
    d4 = [5,3]

    fig = Figure(resolution = (1200, 800),fontsize = 32,colormap = (:Spectral_10, 0.85))  
    
    ax = Axis(fig[1, 1])     
    ax.ylabel = "y label"
    ax.xlabel = "x label"

    ds=(d1,d2,d3,d4)
    
    shift=1
    gap=2
    xs,ys=Vector{Int}(),Vector{Int}()
    xticks=Vector{String}()
    for (i,di) ∈ enumerate(ds)
        b=shift
        shift+=length(di)
        newxs = collect(b:shift-1)
        xs = vcat(xs,newxs) 
        ys = vcat(ys,di)
        xticks = vcat(xticks,[string(i)*","*string(j) for j ∈ eachindex(di)])
        shift += gap
    end

    barplot!(xs,ys, strokewidth = 2,color=xs)
    ax.xticks = (xs, xticks)
    ax.xticklabelrotation = 45
    CairoMakie.save("toto.png",fig)

    fig
end

test_bar()

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 10, 2022, 11:33am UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/3 "2022-10-10T11:33:27Z")

</div>

> [@LaurentPlagne](#):
>
> ```julia
> d1 = [1,3,4,3,2]
> d2 = [2]
> d3 = [3]
> d4 = [5,3]
> 
> ```

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 10, 2022, 3:56pm UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/4 "2022-10-10T15:56:08Z")

</div>

Here’s an example using [Bokeh](https://github.com/cjdoris/Bokeh.jl):

```julia
using Bokeh, DataFrames
# to show plots in the browser
Bokeh.settings!(display="browser")
# flatten the data out into a table, with "i" and "j" columns giving the index of each datum
ds = [[1,3,4,3,2],[2],[3],[5,3]];
df = DataFrame([(; i=string(i), j=string(j), ij=(string(i), string(j)), x) for (i,d) in enumerate(ds) for (j,x) in enumerate(d)]);
# construct the plot
fig = figure(x_range=unique(df.ij));
plot!(fig, VBar, x="ij", top="x", color=factor_cmap("i", "Category10_8", unique(df.i)), source=df);
display(fig)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/9/19b3d1c64eaf6b0ee9d0f0064627a53ef69904cc.png)

Or using the unreleased and highly experimental new [Algebrokeh](https://github.com/cjdoris/Bokeh.jl/Algebrokeh):

```julia
using Bokeh, Algebrokeh, DataFrames
Bokeh.settings!(display="browser", theme="algebrokeh")
# same as before
ds = [[1,3,4,3,2],[2],[3],[5,3]];
df = DataFrame([(; i=string(i), j=string(j), ij=(string(i), string(j)), x) for (i,d) in enumerate(ds) for (j,x) in enumerate(d)]);
# much simpler plotting
plot(df, VBar, x="@ij", y="@x", color="@i")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/5/357e025f03a5acbf0ced9aa7ccb276700acba171.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:** [October 10, 2022, 6:38pm UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/5 "2022-10-10T18:38:42Z")

</div>

A simple way, using Plots.jl’s `bar()`:

```julia
using Plots; gr()

d1, d2, d3, d4 = [1,3,4,3,2], [2], [3], [5,3]

dlist = [d1, d2, d3, d4]
ni = length.(dlist)
dc = distinguishable_colors(maximum(ni), [RGB(0,0,0),RGB(1,1,1)], dropseed=true)
plot(xlabel="Series", ylabel="Values", widen=false)
k = 1
for (n,d) in zip(ni, dlist)
    bar!(k:k+n-1, d, bar_width=1, c=dc[1:n])
    k += n + 1
end
xticks!(cumsum(ni .+ 1) .- (ni .+ 1)/2, ["d1","d2","d3","d4"])

```

 ![Plots_bar_grouped_manually](https://global.discourse-cdn.com/julialang/original/3X/c/8/c803c130dacd9fad628c990ef7eb71c9664f396d.png)

_ **NB:** the colors respect the element position in each vector_

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 11, 2022, 4:16pm UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/6 "2022-10-11T16:16:54Z")

</div>

Thanks both of you! I usually use Plots, so it is nice to be able to do it there 🙂

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 12, 2022, 10:00pm UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/7 "2022-10-12T22:00:14Z")

</div>

A bit late but it took me more work than I thought to make each group of same size regardless the number of bars in them.

```julia
using GMT
bar([2 0.5 1 NaN 1; 4 NaN NaN 2 NaN; 1 3 4 3 2; NaN 3 NaN 1 2],
    fillalpha=[0.3 0.5 0.7], xticks=(:d1, :d2, :d3, :d4), show=true)

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/b/6/b6039cfc85b95c18115f82162869007590f23782.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:** [October 13, 2022, 8:18am UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/8 "2022-10-13T08:18:01Z")

</div>

@joa-quim, fyi, using GMT v0.43.0 and Win11 Julia 1.8.0, the output from your code looks quite different:

 ![GMT_bar_v0.43.0_Julia_1.8.0_Win11_](https://global.discourse-cdn.com/julialang/original/3X/d/6/d654a61b8200d63acc9ec40b164117056e6807ec.png)

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 13, 2022, 12:44pm UTC](https://discourse.julialang.org/t/grouped-bar-plots-with-different-number-of-bars-in-each-group/88517/9 "2022-10-13T12:44:22Z")

</div>

@rafael.guerra , yes I know. You’ll need the master version to reproduce my plot. Unfortunately GMT is blocked from AutoMerge due to the libstdc++ dependency conflict [issue](https://github.com/JuliaLang/julia/pull/45582)

You will get something more similar if you do

```julia
bar(1:4, [2 0.5 1 NaN 1; 4 NaN NaN 2 NaN; 1 3 4 3 2; NaN 3 NaN 1 2],
    fillalpha=[0.3 0.5 0.7], xticks=(:d1, :d2, :d3, :d4), show=true)

```

(the need to specify the x coordinates when `xticks` kw is used was also droped in my recent changes).
