VegaLite: Text labels on bar charts

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.

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

testplot2(df,:TP) # text labels

image

However, combining only shows one layer(?)

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.

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

mp = @vlplot() + bp + tp

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

Thanks!

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