# Use Julia to plot grouped bar chart

**URL:** <https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [June 12, 2024, 1:54pm UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554 "2024-06-12T13:54:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Z\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/z_m/32/52830_2.png) [@Z\_M](https://discourse.julialang.org/u/Z_M)\
**Post date:** [June 12, 2024, 1:54pm UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554/1 "2024-06-12T13:54:14Z")

</div>

Hi guys!  
I want to know how to use Julia to plot a bar chart like the following picture.  
Specifically, each X-axis label corresponds to two or more columns.  
Thank you very much!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4fb91d885cc1e525159358ae09d9031220e0c7aa.png)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 12, 2024, 2:52pm UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554/2 "2024-06-12T14:52:22Z")

</div>

Depends on which plotting package you want to use.

Easiest (IMHO) is AoG with a matching example in the tutorial at

[https://aog.makie.org/stable/generated/penguins/](https://aog.makie.org/stable/generated/penguins/)

---

<div class="post-metadata">

**Author:** ![Z\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/z_m/32/52830_2.png) [@Z\_M](https://discourse.julialang.org/u/Z_M)\
**Post date:** [June 13, 2024, 4:04am UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554/3 "2024-06-13T04:04:50Z")

</div>

Thank you very much!

I use this package, but I still have some problems.

My data is as follows:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/a/9af5affb2e57b49f461ce1b8decf4268e759fee7.png)

My code and the result are as follows:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/a/7a0a3fb66e5eb8be3f4bc4268a003016afce9462.png)

I want to know how to modify the code to display the value of the column " Amount ", rather than the frequency of " Amountof ".

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [June 13, 2024, 9:03am UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554/4 "2024-06-13T09:03:04Z")

</div>

This is the example from the [Makie docs](https://docs.makie.org/stable/reference/plots/barplot#barplot):

```julia
using CairoMakie
tbl = (cat = [1, 1, 1, 2, 2, 2, 3, 3, 3],
       height = 0.1:0.1:0.9,
       grp = [1, 2, 3, 1, 2, 3, 1, 2, 3],
       )

barplot(tbl.cat, tbl.height,
        dodge = tbl.grp,
        color = tbl.grp,
        axis = (xticks = (1:3, ["left", "middle", "right"]),
                title = "Dodged bars"),
        )

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 13, 2024, 12:16pm UTC](https://discourse.julialang.org/t/use-julia-to-plot-grouped-bar-chart/115554/5 "2024-06-13T12:16:06Z")

</div>

Running with Jakob’s example:

```julia
julia> tbl = (cat = [1, 1, 1, 2, 2, 2, 3, 3, 3],
                           height = 0.1:0.1:0.9,
                           grp = [1, 2, 3, 1, 2, 3, 1, 2, 3],
                           )
(cat = [1, 1, 1, 2, 2, 2, 3, 3, 3], height = 0.1:0.1:0.9, grp = [1, 2, 3, 1, 2, 3, 1, 2, 3])

julia> plt = data(tbl) * mapping(:cat, :height) * mapping(dodge = :grp => nonnumeric) * mapping(color = :grp => nonnumeric) * visual(BarPlot)
Layer(AlgebraOfGraphics.Visual(Plot{Makie.barplot}, {}), (cat = [1, 1, 1, 2, 2, 2, 3, 3, 3], height = 0.1:0.1:0.9, grp = [1, 2, 3, 1, 2, 3, 1, 2, 3]), Any[:cat, :height], {:dodge = :grp => AlgebraOfGraphics.nonnumeric, :color = :grp => AlgebraOfGraphics.nonnumeric})

julia> fig = AlgebraOfGraphics.draw(plt)

```

The issue with your code is that you are calling `frequency` instead of `mapping(x, y)`.
