# VegaLite: Text labels on bar charts

**URL:** <https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [March 16, 2020, 4:51pm UTC](https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048 "2020-03-16T16:51:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hellemo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellemo/32/324_2.png) [@hellemo](https://discourse.julialang.org/u/hellemo)\
**Post date:** [March 16, 2020, 4:51pm UTC](https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048/1 "2020-03-16T16:51:17Z")

</div>

I can’t figure out how to show the actual values on a bar chart in VegaLite, I tried adapting the examples but clearly I don’t understand what options are necessary to achieve what I want.

I’m able to do each layer more or less how I want it, but I don’t understand how can I get them overlayed and with independent coloring.

```julia
using VegaLite, Random, DataFrames
function syntdata(N=50)
    Random.seed!(1);
    df = DataFrame()
    df.Cat1 = [rand([:A,:B,:C]) for _ = 1:N]
    df.Cat2 = [rand([:D,:E]) for _ = 1:N]
    df.Cat3 = [rand([0,1]) for _ = 1:N]
    return df
end

function testplot2(df,t=:BP)
    bp = @vlplot(:bar, #barplot
            x={aggregate="count",field="Cat3:n",stack="normalize"},
            y="Cat1:n",
            color="Cat3:n") 
    tp = @vlplot(:text, # text labels
            x={aggregate="count",field="Cat3:n",stack="normalize"},
            y="Cat1:n",
            color="Cat3:n",
            text="count(Cat3)"
        )

    if t == :BP
        mp= bp
    elseif t ==:TP
        mp = tp
    else
        mp = bp + tp
    end

    df |> @vlplot(
        facet={row={field="Cat2", type="nominal"}}
    ) + 
    (
        mp
    )
end

df = syntdata()
testplot2(df,:BP) # only bar

```

![image](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d4fd32fc65be9c38857565cbf879e160351e0cd.png)

```julia
testplot2(df,:TP) # text labels

```

![image](https://global.discourse-cdn.com/julialang/original/3X/d/6/d61e87fb487864e805d1d2875d65d87288d6c327.png)

However, combining only shows one layer(?)

```julia
testplot2(df,:Both)

```

Output is similar to the last one.

I tried several things, I think i may need `config={resolve={scale={color="independent"}}}` to get different text color for example, but tried to make the example above as simple as possible while maintaining the same structure, but I don’t understand what I need to do to make the text overlay the bars.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 16, 2020, 6:09pm UTC](https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048/2 "2020-03-16T18:09:29Z")

</div>

I think if you change the line that does the composition to

```julia
mp = @vlplot() + bp + tp

```

it should work. I have to admit I don’t fully understand why right now 🙂

---

<div class="post-metadata">

**Author:** ![hellemo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellemo/32/324_2.png) [@hellemo](https://discourse.julialang.org/u/hellemo)\
**Post date:** [March 16, 2020, 6:38pm UTC](https://discourse.julialang.org/t/vegalite-text-labels-on-bar-charts/36048/3 "2020-03-16T18:38:04Z")

</div>

Thanks!

I don’t understand, but am happy to get it working atm 😄
