# Plotting by multiple factors

**URL:** https://discourse.julialang.org/t/plotting-by-multiple-factors/68237
**Category:** General Usage
**Tags:** question, plotting, dataframes, statsplots
**Created:** [September 15, 2021, 11:09pm UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237 "2021-09-15T23:09:23Z")
**Posts on this page:** 1
**Showing post:** 3

<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: [September 16, 2021, 8:57am UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237/3 "2021-09-16T08:57:41Z")

</div>

You can use grouped dataframes and loop over these.  
As there are only two groups in your example, the code below writes it explicitly without a loop:

```julia
gdf = groupby(df, :function)
fig = plot(layout=(2,1), legend=:topleft)
@df gdf[1] plot!(fig[1],:x, :y, group=(:function, :p1, :p2), ylims=(0,20))
@df gdf[2] plot!(fig[2],:x, :y, group=(:function, :p1, :p2), ylims=(0,20))

```

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

---

_[View the full topic](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237)._
