# VegaLite: facet plot with running average layer

**URL:** <https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463>\
**Category:** Visualization\
**Created:** [March 3, 2020, 1:30pm UTC](https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463 "2020-03-03T13:30:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [March 3, 2020, 1:30pm UTC](https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463/1 "2020-03-03T13:30:45Z")

</div>

I have a dataset with continuous values and would like to plot those values together with their running averages in a layered plot. Works well in this simple case:

```julia
using Random, DataFrames, Queryverse
Random.seed!(1234)
df = DataFrame(index=1:100, value=rand(100))

df |> @vlplot(transform=[{frame= [-15, 15], window=[{op=:mean, field=:value, as=:avg_value}]}]) +
@vlplot(
    mark={:line, strokeWidth=2, opacity=0.5},
    x="index:q",
    y="value:q"
) +
@vlplot(
    mark={:line, strokeWidth=3},
    x="index:q",
    y="avg_value:q"
)

```

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

However, if I now would like to stratify the plot according to a categorical variable, the following fails:

```julia
df[!, :category] .= rand(1:2, 100)

df |> @vlplot(facet={column={field="category", typ="nominal"}}) +
(
    @vlplot() +
    @vlplot(
        mark=:line,
        x="index:q",
        y="value:q",
    ) + @vlplot(
        mark={:line, strokeWidth=3},
        transform=[{frame= [-15, 15], window=[{op=:mean, field=:value, as=:avg_value}]}],
        x="index:q",
        y={field=:avg_value, typ=:quantitative, aggregate=:mean},
        color={value=:red}
    )
)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/f/8f5815febbf2bf1b32315d66add8382e1a117c37.png) […]  
Putting the `transform` statement in other places (for instance the initial `@vlplot()`'s) unfortunately results in the same error. This simplified, transformation-less version however works:

```julia
df |> 
@vlplot(facet={column={field="category", typ="nominal"}}) +
(
    @vlplot() +
    @vlplot(
        mark=:line,
        x="index:q",
        y="value:q",
    ) + @vlplot(
        mark={:rule, strokeWidth=3},
        y={field=:value, typ=:quantitative, aggregate=:mean},
        color={value=:red}
    )
)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/9/2/922b42ce79e9b8ae8299f5903fd71adf571bd5bc.png)

Any idea what I’m doing wrong?

---

<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:** [March 4, 2020, 11:08am UTC](https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463/2 "2020-03-04T11:08:12Z")

</div>

It’s not that you are are doing it wrong, or at least, I can’t tell where it is done wrong.

I needed some time with trial and error to develop the following suggestion based on your code:

```julia
df |>
@vlplot(
    transform=[{
        frame= [-15, 15], 
        window=[{op=:mean, field=:value, as=:avg_value}],
        groupby=["category"]
    }],
    facet={column={field="category", type="nominal"}}
) +
(
    @vlplot() +
    @vlplot(
        mark=:line,
        x="index:q",
        y="value:q"
    ) +
    @vlplot(
        mark={:line, strokeWidth=3},
        x="index:q",
        y={field=:avg_value, type="quantitative", aggregate=:mean},
        color={value=:red}
    )
)

```

I didn’t check if the calculated mean values (red line) are correct!  
 ![facet](https://global.discourse-cdn.com/julialang/original/3X/8/5/85015b4fa546b5365d5ed85df42584a1bdcae56e.jpeg)

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [March 4, 2020, 11:46am UTC](https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463/3 "2020-03-04T11:46:35Z")

</div>

Thanks a lot, works like a charm! I thought I had tried putting the transform there before, but apparently not :). Out of curiosity, does the `groupby` ensure that the transformation window is only applied per group?

---

<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:** [March 4, 2020, 12:19pm UTC](https://discourse.julialang.org/t/vegalite-facet-plot-with-running-average-layer/35463/4 "2020-03-04T12:19:05Z")

</div>

> [@jtackm](#):
>
> …does the `groupby` ensure that the transformation window is only applied per group?

I hope that. That is what I meant with “i didn’t check the mean values”.  
Thats how I interprete:

> groupby String [] The data fields for partitioning the data objects into separate windows. If  
> unspecified, all data points will be in a single window.

from here

> **[Window](https://vega.github.io/vega-lite/docs/window.html)**
>
> Vega-Lite - a high-level grammar for statistical graphics. Vega-Lite provides a higher-level grammar for visual analysis, comparable to ggplot or Tableau, that generates complete Vega specifications. Vega-Lite specifications consist of simple...
