# 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:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [September 15, 2021, 11:09pm UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237/1 "2021-09-15T23:09:23Z")

</div>

Hi all,

I have a dataset with three factors and one dependent variable. In the example below, how do I get all of the linear data in the top plot and all of the exponential data in the bottom plot. I tried changing the order of variables in group, but to no avail.

**Example**

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

**Code**

```julia
using StatsPlots, DataFrames

linear(x, β0, β1) = β0 + β1 * x

exponential(x, β, λ) = β * exp(λ * x)

xs = 1:10

np1 = [(p1,p2,x,y = linear(x, p1, p2)) for p1 in [0.0,1.0] for p2 in [.2,.6] for x in xs]

df = DataFrame(np1)
df[!,:function] .= "linear"

np2 = [(p1,p2,x,y = exponential(x, p1, p2)) for p1 in [.0002,.0003] for p2 in [1.0,1.1] for x in xs]
temp = DataFrame(np2)
temp[!,:function] .= "exponential"

append!(df, temp)

@df df plot(:x, :y, group=(:p1,:p2,:function), layout=(2,1), ylims=(0,20))

```

---

<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)

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [September 16, 2021, 9:14am UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237/4 "2021-09-16T09:14:16Z")

</div>

Thank you for your solution. This is a good workaround.

I wonder if it would be possible to add a feature to StatsPlots that organizes the factors based on the layout and group arguments. I suppose it might be hard to generalize a pattern.

---

<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, 11:23am UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237/5 "2021-09-16T11:23:33Z")

</div>

Someone more knowledgeable should advise.  
At any rate, do not see harm in opening an [issue at StatsPlots.jl,](https://github.com/JuliaPlots/StatsPlots.jl/issues) in case there is really one and improvements can be made as you suggest.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [September 16, 2021, 11:42am UTC](https://discourse.julialang.org/t/plotting-by-multiple-factors/68237/6 "2021-09-16T11:42:25Z")

</div>

I opened an [issue](https://github.com/JuliaPlots/StatsPlots.jl/issues/466). Thanks again for your help!
