# Multiple subplots using StatsPlots

**URL:** <https://discourse.julialang.org/t/multiple-subplots-using-statsplots/37990>\
**Category:** Visualization\
**Created:** [April 21, 2020, 8:43pm UTC](https://discourse.julialang.org/t/multiple-subplots-using-statsplots/37990 "2020-04-21T20:43:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)\
**Post date:** [April 21, 2020, 8:43pm UTC](https://discourse.julialang.org/t/multiple-subplots-using-statsplots/37990/1 "2020-04-21T20:43:46Z")

</div>

Hello,

splitting my hair around this and now I need some help. I’m sure it’s straightforward, but I can’t find the answer!

Here’s a MWE. It’s a list of subplots that creates multiple `qqplot` over random values.

```julia
using StatsPlots
using Random

x = rand(1000)
y = rand(1000)

StatsPlots.plot(
    qqplot(x, y),

    qqplot(x,y))

```

It creates the following figure:  
 ![qqplot1](https://global.discourse-cdn.com/julialang/original/3X/d/1/d12db30361f9877e28a8c877dc24c3e1b97cb5b6.png)

Now, I’m trying to add a fitted lines on each subplot. Here’s my code and what it produce (trying only on 1st subplot for this example).

```julia
using Polynomials
using Statistics

xq = quantile(x, range(0.0, stop=1.0, length=300))
yq = quantile(y, range(0.0, stop=1.0, length=300))

t1 = fit(xq, yq)

StatsPlots.plot(
    plot(qqplot(x, y),
    plot(t1,0.0:1.0, label=string("Slope = ", round(t1.coeffs[2], digits=3)))),

    qqplot(x,y))

```

![qqplot2](https://global.discourse-cdn.com/julialang/original/3X/3/0/309c1522f46d56db1d52b1b5b9ed82b7fdaa343e.png)

I tried with different combination where inside the 1st subplot I use `plot!`, but without success.

Any help would be appreciated!

---

<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:** [April 21, 2020, 9:34pm UTC](https://discourse.julialang.org/t/multiple-subplots-using-statsplots/37990/2 "2020-04-21T21:34:39Z")

</div>

You can decide to show the fitted line by using `qqline = :fit`.  
But if you want to do it manually, you should build up each plot separately. So in principle:

```julia
p1 = qqplot(x,y)
plot!(p1, t1,0.0:1.0, label=string("Slope = ", round(t1.coeffs[2], digits=3)))

StatsPlots.plot(
    p1,
    qqplot(x,y))

```

This will not work though because `t1` seems to define the wrong values.

---

<div class="post-metadata">

**Author:** ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)\
**Post date:** [April 22, 2020, 12:20am UTC](https://discourse.julialang.org/t/multiple-subplots-using-statsplots/37990/3 "2020-04-22T00:20:32Z")

</div>

ok, thanks!

it does work, as you proposed, when I do it manually.

here’s the outcome:

```julia
using StatsPlots
using Random
using Polynomials
using Statistics

x = rand(1000)
y = rand(1000)

xq = quantile(x, range(0.0, stop=1.0, length=300))
yq = quantile(y, range(0.0, stop=1.0, length=300))

t1 = fit(xq, yq)
t2 = fit(xq, yq)

p1 = qqplot(x, y, qqline=:fit)
plot!(p1, t1, 0.0:1.0)

p2 = qqplot(x, y)
plot!(p2, t2, 0.0:1.0)

# Final plot, with subplots
plot(p1, p2, layout = (1,2))

```

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

Of course, the figure makes more sense with actual values (they are far from the identity line).

Thanks again!
