# Plotting "hierachically" organized variables

**URL:** <https://discourse.julialang.org/t/plotting-hierachically-organized-variables/104978>\
**Category:** Visualization\
**Tags:** makie, algebraofgraphics\
**Created:** [October 15, 2023, 1:08pm UTC](https://discourse.julialang.org/t/plotting-hierachically-organized-variables/104978 "2023-10-15T13:08:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [October 15, 2023, 1:08pm UTC](https://discourse.julialang.org/t/plotting-hierachically-organized-variables/104978/1 "2023-10-15T13:08:16Z")

</div>

Dear community,  
is there an elegant/easy/concise way of plotting some y variables against an x variable, where y variables should either be divided into facets or colors, i.e.

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

for a dataframe and defined facets:

```julia
a=randn(10);
b = a .+ 1;
c = 2a

d = randn(10);
e = d .-1

df = DataFrame(;time=1:length(a), a, b, c, d, e)
facets=[[:a, :b, :c], [:d, :e]]

```

I came up with a cumbersome(?) function:

```julia
function vari_facets(df, x, facets)
    function assign_facet(v)
        fidx = findall(facets) do fa
            v in String.(fa)
        end
        fidx = isnothing(fidx) ? missing : fidx
        return fidx
    end
    dflong = DataFrames.stack(df, Not(x))
    
    @transform! dflong @astable :facet = assign_facet(:variable)
    print(dflong)

    plts = map(enumerate(facets)) do (i, fa)
        p = data(@subset(dflong |> dropmissing, i in :facet)) * mapping(x, :value, color=:variable) * visual(Lines)
     end
    
     fig=Figure(resolution=(1920,1080))
    
     for i in 1:length(plts)
        g=draw!(fig[i,1], plts[i], axis=(; xlabelvisible=i == length(plts)))
        legend!(fig[i,2],g)
     end
    fig
end

vari_facets2(df, :time, facets)

```

which creates the figure above.

Is there are better more idiomatic way I might be missing?  
Thank!

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 15, 2023, 4:20pm UTC](https://discourse.julialang.org/t/plotting-hierachically-organized-variables/104978/2 "2023-10-15T16:20:03Z")

</div>

Coming from _ggplot_ I like `AlgebraOfGraphics` for such types of things:

```julia
using AlgebraOfGraphics, CairoMakie

tofacet(x) = "Facet_" * string(findfirst(in.(Symbol(x), facets)))
data(DataFrames.stack(df, Not(:time))) * mapping(:time, :value, color = :variable, row = :variable => tofacet) * visual(Lines) |> draw

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 15, 2023, 11:44pm UTC](https://discourse.julialang.org/t/plotting-hierachically-organized-variables/104978/3 "2023-10-15T23:44:38Z")

</div>

Sorry, missed that you already use `AlgebraOfGraphics` and ignored your requirement to get a legend per group. Don’t think there is a simpler solution than building the plots per group and drawing the respective subplots explicitly.  
The first task can be simplified using the [wide data](https://aog.makie.org/stable/gallery/gallery/data%20manipulations/wide_data/#Wide-data) specification though:

```julia
# This comprehension replaces all of your function till `fig=...`
plts = [data(df) * mapping(:time, grp .=> :value, color = dims(1) => renamer(grp)) * visual(Lines)
        for grp in facets]

```

Hope that actually helps.
