# Automating the Best Layout for Plotting Variables?

**URL:** <https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [February 3, 2024, 4:24pm UTC](https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672 "2024-02-03T16:24:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 3, 2024, 4:24pm UTC](https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672/1 "2024-02-03T16:24:31Z")

</div>

Hi folks!

I have been struggling with a problem on how to best plot variables that are not always guaranteed in data. Let me show an example of what I am talking about:

Suppose I have a dataframe that looks something like this:

```julia
10×4 DataFrame
 Row │ age_group gender_concept_id race_concept_id count
     │ String? Int32? Int32? Int64
─────┼──────────────────────────────────────────────────────
   1 │ 50 - 59 8532 8527 403
   2 │ 40 - 49 8532 8515 51
   3 │ 30 - 39 8507 8516 20
   4 │ 50 - 59 8507 8527 445
   5 │ 40 - 49 8532 8516 59
   6 │ missing 8532 8527 139
   7 │ 60 - 69 8532 8516 43
   8 │ 50 - 59 8532 8516 69
   9 │ 80 - 89 8532 8527 102
  10 │ 30 - 39 8507 8527 114

```

Then, I have a bespoke plotting function that will generate a plot that looks something like this which contains multiple subplots:

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

Then, suppose that I receive a similar dataframe that only has age group and count. I’d then want to subset the data like this with my function and generate one plot:

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

I have been exploring around how one might create a function mechanism using the idea of faceting that allows one to input a dataframe to a function built around something like AlgebraOfGraphics that seems to enable one the ability to loosely do this automatic plotting approach. But I am still not sure; I do realize that this problem is asking for a solution that does a bit of mind-reading but it feels like some aspects of it should be able to be automated.

Does anyone have any ideas on how to approach this and if AlgebraOfGraphics might be a good place to start looking? Happy to provide more details as it is a bit of a vague problem description.

Cheers!

~ tcp 🌳

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 3, 2024, 6:28pm UTC](https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672/2 "2024-02-03T18:28:11Z")

</div>

First of all it sounds like you need a mechanism to update the data of a plot or some derivative of the data.

You may be interested in Observables.jl (or alternatively Reactive.jl). This allows you to update a plot when data is updated.

Next I think you should consider Makie’s Observables interaction:

[https://docs.makie.org/stable/explanations/nodes/](https://docs.makie.org/stable/explanations/nodes/)

Finally, to do the final integration you can look at Makie’s recipes system.

[https://docs.makie.org/stable/explanations/recipes/](https://docs.makie.org/stable/explanations/recipes/)

---

<div class="post-metadata">

**Author:** ![jerlich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerlich/32/5205_2.png) [@jerlich](https://discourse.julialang.org/u/jerlich)\
**Post date:** [February 3, 2024, 10:50pm UTC](https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672/3 "2024-02-03T22:50:45Z")

</div>

It is hard to imagine that one can automate this in a good way. How would your code know which factor to set as colour, which to facet as row, column, etc.

I guess one could look at the number of unique values in each column?

Algebra of Graphics does a pretty good job at making it easy to go from DataFrame to faceted plot (not quite as good as ggplot, but getting there).

I think you can build up the mapping element by element. So you could have lines which are like `:x in names(df) && (mapping *= mapping(row=:x))`. Note: I haven’t tested this, but from the examples, it seems you can.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [February 4, 2024, 2:57pm UTC](https://discourse.julialang.org/t/automating-the-best-layout-for-plotting-variables/109672/4 "2024-02-04T14:57:37Z")

</div>

If you are willing to provide the algorithm the list of _potential_ grouping variables, the solution that I use works basically as follows:

1. User provides a vector of grouping variables. That could be length 1, 2, or 3. Of course, the algorithm could also take a vector of length 3 and just ignore variables that don’t exist in your DataFrame.
2. The first variable becomes the color argument of the AoG `mapping`. I have a function `color_mapping(colorVar)` that returns `mapping(color = ...)` or just `mapping()` when there is grouping (a single series).
3. The second variable becomes the layout mapping. There is again a function `layout_mapping(layoutVar)` which returns either `mapping(layout = dims(1) => ...)` or just `mapping()` if there are not subplots.
4. The third variable handles sub-figures, if needed.

Now you can assemble your plot by chaining the (x,y) mapping \* color mapping \* layout mapping.
