VegaLite: Facet plot with errorband

Does VegaLite not support faceting of plots with errorbands? I’m struggling to make this work:

using VegaLite, DataFrames

function generatedata()
    df = DataFrame()
    K = 10
    L = 10
    Cat1 = [:A,:B]
    Cat2 = [:X,:Y]
    for i in Cat1, j in Cat2
        tmp = DataFrame()
        tmp.Cat1 = [i for _ in 1:K*L]
        tmp.Cat2 = [j for _ in 1:K*L]
        tmp.Week = [rem(w,K) for w in 1:K*L]
        df = vcat(df,tmp)
    end
    df.Id = [i for i in 1:size(df,1)]
    df.Value = rand(size(df,1))
    df[df.Cat1.==:A,:Value] .+=1
    df[df.Cat2.==:X,:Value] .-=2
    return df
end

df = generatedata()

# Works (Error band + color + mean)
df |> @vlplot(x=:Week,color=:Cat2) + @vlplot(mark={:errorband, extent=:ci},y=:Value) + @vlplot(:line,y="mean(Value)")
# Facet version of this does not work:
df |> @vlplot(x=:Week,color=:Cat2,column=:Cat1) + @vlplot(mark={:errorband, extent=:ci},y=:Value) + @vlplot(:line,y="mean(Value)")
# Works (simple line version)
df |> @vlplot(:line,x=:Week,color=:Cat1,column=:Cat2,y="mean(Value)")

image

We had a similar question here

The solution wasn’t obvious so maybe it is similar here. Could you try for yourself? I could perhaps look later into details.

1 Like

This works for me (VegaLite 1.0.0):

df |>
@vlplot(
    facet={column={field="Cat1", type="nominal"}}
) + 
(
    @vlplot(x="Week:q",color="Cat2:n") + 
    @vlplot(mark={:errorband, extent=:ci},y="Value:q") + 
    @vlplot(:line,y="mean(Value)")
)

image

1 Like

Brilliant! That works for me! Thanks a lot @jtackm! I also found the answer that @oheil mentioned, but wasn’t able to map it to my case last night. Thanks both of you!

2 Likes

On faceting, you can see my problem and solution which may be applicable to you.

VegaLite throws error on faceting plots

Although I am not sure if the behaviour has changed with the newest release.