# StatsPlots & DataFrames: grouping by multiple columns

**URL:** <https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364>\
**Category:** Visualization\
**Tags:** dataframes, statsplots\
**Created:** [February 13, 2022, 6:25pm UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364 "2022-02-13T18:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cingulate7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cingulate7/32/33786_2.png) [@Cingulate7](https://discourse.julialang.org/u/Cingulate7)\
**Post date:** [February 13, 2022, 6:25pm UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364/1 "2022-02-13T18:25:23Z")

</div>

I want to create a box plot of data in a DataFrame grouped by more than one variable (column value). Is there a compact high-level way to do this? The following does not work:

‘’'using DataFrames, StatsPlots

dfa = DataFrame(a = [0, 1, 0, 1, 0, 1, 0, 1],  
b = [1, 1, 1, 1, 2, 2, 2, 2],  
dat = randn(8))

@df dfa boxplot(:dat, group=([:a, :b]))‘’’

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 13, 2022, 8:52pm UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364/2 "2022-02-13T20:52:47Z")

</div>

Not sure if this is what you need, but just in case:

```julia
using DataFrames, StatsPlots

dfa = DataFrame(a = [0, 1, 0, 1, 0, 1, 0, 1],
b = [1, 1, 1, 1, 2, 2, 2, 2],
dat = randn(8))

gdf = groupby(dfa, [:a, :b])
nt = NamedTuple.(keys(gdf))
plot(legend=:outertopright)
i = 1
for (k,v) in pairs(gdf)
    @df v boxplot!(:dat, label="$(nt[i])")
    i += 1
end
Plots.current()

```

 ![StatsPlot_Boxplot_grouped](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4378618dfe3f84189c0d210c7d033346be83fd3.png)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 14, 2022, 8:58am UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364/3 "2022-02-14T08:58:24Z")

</div>

AFAIC `@df dfa boxplot(:dat, group=(:a, :b))` should work and it’s a big if it doesn’t

---

<div class="post-metadata">

**Author:** ![Cingulate7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cingulate7/32/33786_2.png) [@Cingulate7](https://discourse.julialang.org/u/Cingulate7)\
**Post date:** [February 17, 2022, 1:36am UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364/4 "2022-02-17T01:36:33Z")

</div>

It does not work properly - although there are 4 categories (which is accurately reflected in the legend), it generates 8 bars (one for each data point):

 ![Screen Shot 2022-02-16 at 6.34.53 PM](https://global.discourse-cdn.com/julialang/original/3X/5/a/5a21e514907f7fcb966b55a47cf92047785ba212.png)

---

<div class="post-metadata">

**Author:** ![Cingulate7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cingulate7/32/33786_2.png) [@Cingulate7](https://discourse.julialang.org/u/Cingulate7)\
**Post date:** [February 17, 2022, 4:33pm UTC](https://discourse.julialang.org/t/statsplots-dataframes-grouping-by-multiple-columns/76364/5 "2022-02-17T16:33:51Z")

</div>

Someone kindly gave an answer on GitHub - the first argument needs to be the labels of the groups:

@df df boxplot(string.(tuple.(:a, :b)), :dat, group=(:a, :b))
