# Grouping in Julia plots

**URL:** <https://discourse.julialang.org/t/grouping-in-julia-plots/41596>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [June 17, 2020, 2:18pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596 "2020-06-17T14:18:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [June 17, 2020, 2:18pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/1 "2020-06-17T14:18:31Z")

</div>

Hi there,

Let’s say I have a dataframe I want to plot. I know that I can use the keyword argument `group` to select a column and to have a grouping for the different cases. But what if I would like to manually group together different cases, e.g. group cases `A` and `B` and cases `C` and `D`?

Many thanks in advance.

```julia
using DataFrames, Plots
DF = DataFrame(x = 1:4, y = rand(4),cases=["A","B","C","D"]) # These are the plotting data
plot(DF.x,DF.y, group=DF.cases,seriestype = :scatter)

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 17, 2020, 2:27pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/2 "2020-06-17T14:27:39Z")

</div>

Welcome!

I think the easiest thing to do here is to make a new grouping column.

```julia
df.combined_cases = map(df.cases) do c
    if c in ["A", "B"]
        "A or B"
    elseif c in ["C", "D"]
        "C or D"
    else
        c
    end
end
```

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [June 17, 2020, 2:40pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/3 "2020-06-17T14:40:31Z")

</div>

The answer depends on what you are trying to show. Could you provide a link to an example plot?

You might consider using the `layout` functionality to generate two subplots. One with A and B, and the other with C and D, e.g.:

```julia
plot(plot_ab, plot_cd, layout=(1,2))

```

But this might not be what you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [June 17, 2020, 2:54pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/4 "2020-06-17T14:54:13Z")

</div>

Thanks, I guess that would be the sane option. But some subsetting directly at the group argument is not favorable or possible? Something like…

```julia
plot(DF.x,DF.y, group=DF[:cases .=="A" || :cases .=="B",:] ,seriestype = :scatter)

```

…?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 17, 2020, 2:57pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/5 "2020-06-17T14:57:32Z")

</div>

That’s certainly feasible. But you would lose nice automatic labeling.

---

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [June 17, 2020, 2:57pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/6 "2020-06-17T14:57:54Z")

</div>

Thanks. It’s an option, but I guess the least favorable as this maybe somehow deviates from the intended use cases of layout…

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 17, 2020, 3:01pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/7 "2020-06-17T15:01:14Z")

</div>

Why not just write a little helper function? That way you can call it at the function call.

```julia
function combine_cases(x)
    combined_cases = map(df.cases) do c
        if c in ["A", "B"]
            "A or B"
        elseif c in ["C", "D"]
            "C or D"
        else
            c
        end
    end
end

plot(df.x, df.y, group = combined_cases(df.cases))

```

---

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [June 17, 2020, 3:15pm UTC](https://discourse.julialang.org/t/grouping-in-julia-plots/41596/8 "2020-06-17T15:15:40Z")

</div>

Ah! Of course. Encapsulating. Feels like a good choice. Thanks again!

Had to make a few minor corrections to make it work:

```julia
function combine_cases(x)
    combined_cases = map(x.cases) do c
        if c in ["A", "B"]
            "A or B"
        elseif c in ["C", "D"]
            "C or D"
        else
            c
        end
    end
end

plot(DF.x, DF.y, group = combine_cases(DF))

```
