# VegaLite.jl Grouped Bar Chart with Labels

**URL:** https://discourse.julialang.org/t/vegalite-jl-grouped-bar-chart-with-labels/55196
**Category:** New to Julia
**Tags:** question, plotting
**Created:** [February 13, 2021, 9:41am UTC](https://discourse.julialang.org/t/vegalite-jl-grouped-bar-chart-with-labels/55196 "2021-02-13T09:41:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sjvrensburg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sjvrensburg/32/21945_2.png) [@sjvrensburg](https://discourse.julialang.org/u/sjvrensburg)
#### Post date: [February 13, 2021, 9:41am UTC](https://discourse.julialang.org/t/vegalite-jl-grouped-bar-chart-with-labels/55196/1 "2021-02-13T09:41:15Z")

</div>

I am having some issues trying to create a grouped bar chart with labels using `VegaLite.jl`. From [this answer on Discoure,](https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048/2) it looks like I should be able to just “sum” two plots.

However, the following doesn’t work:

```julia
using VegaLite, VegaDatasets

bplot = dataset("population") |>
@vlplot(
    :bar,
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex == 2 ? 'Female' : 'Male'", as="gender"}
    ],
    column="age:o",
    y={"sum(people)", axis={title="population", grid=false}},
    x={"gender:n", axis={title=""}},
    color={"gender:n", scale={range=["#675193", "#ca8861"]}},
    spacing=10,
    config={
        view={stroke=:transparent},
        axis={domainWidth=1}
    }
)

tplot = dataset("population") |>
@vlplot(
    mark={
        :text,
        dx=-5,
        angle=90,
        align="right",
        baseline="middle",
        dx=-5
        },
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex == 2 ? 'Female' : 'Male'", as="gender"}
    ],
    column="age:o",
    y={"sum(people)", axis={title="population", grid=false}},
    x={"gender:n", axis={title=""}},
    text="age",
    spacing=10,
    config={
        view={stroke=:transparent},
        axis={domainWidth=1}
    })

@vlplot() + bplot + tplot

```

It appears that `@vlplot() + bplot + tplot` removes the groups.

I came across [this answer on stackoverflow.com](https://stackoverflow.com/a/62116925/2436459), but I don’t quite know how to “translate” the VegaLite JSON to Julia. I also don’t really want to work with JSON.

Also, I’m not deadset on using `VegaLite.jl`. If someone could maybe explain how one might accomplish this with `Plots.jl` or `StatsPlots.jl` then I’d be happy too.

Any help, whatsoever, would be much appreciated.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [February 13, 2021, 6:17pm UTC](https://discourse.julialang.org/t/vegalite-jl-grouped-bar-chart-with-labels/55196/2 "2021-02-13T18:17:35Z")

</div>

This is a working translation of the stackoverflow solution:

```julia
bplot = dataset("population") |>
@vlplot(
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex == 2 ? 'Female' : 'Male'", as="gender"}
    ],
    spacing=10,
    facet={field="age",type="ordinal"},
	spec={
		encoding={
			y={"sum(people)", axis={title="population", grid=false}},
			x={"gender:n", axis={title=""}},
		},
		layer=[{
			:bar,
			color={"gender:n", scale={range=["#675193", "#ca8861"]}},
		},
		{
			text="age",
			mark={
				:text,
				dx=-5,
				angle=90,
				align="right",
				baseline="middle",
				dx=-5
				},
		}]
	},
    config={
        view={stroke=:transparent},
        axis={domainWidth=1}
    }
)

```

For the reason why your attempt doesn’t work I can only speculate a bit:  
In the single grouped bar char [Bar Charts · VegaLite.jl](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_barcharts/#Grouped-Bar-Chart-1) the line `column="age:o"` defines that we want facets plotted as columns.  
By adding your two plots this information gets lost, because the x-axis has to be equal for the two plots and is defined as only `x="gender:n"` which is exactly two values on x. It would translate into:

```julia
dataset("population") |>
@vlplot(
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex == 2 ? 'Female' : 'Male'", as="gender"}
    ],
    x={"gender:n", axis={title=""}}
) + @vlplot(
    :bar,
    ...
) + @vlplot(
    mark={
        :text,
        ....
)

```

The above VegaLite.jl solution just makes the `facet` and the `spec` explicit to reproduce the original solution. I tried with `column` and several shortcuts, but they didn’t work. A shortcut for example I thought should work would be:

```julia
...
facet="age:o",
...

```

but don’t work.

---

<div class="post-metadata">

### Author: ![sjvrensburg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sjvrensburg/32/21945_2.png) [@sjvrensburg](https://discourse.julialang.org/u/sjvrensburg)
#### Post date: [February 14, 2021, 8:18am UTC](https://discourse.julialang.org/t/vegalite-jl-grouped-bar-chart-with-labels/55196/3 "2021-02-14T08:18:58Z")

</div>

Awesome! Thank you, @oheil, that worked.
