Multiple subplots using StatsPlots

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.

using StatsPlots
using Random

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

StatsPlots.plot(
    qqplot(x, y),

    qqplot(x,y))

It creates the following figure:
qqplot1

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

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

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

Any help would be appreciated!

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:

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.

1 Like

ok, thanks!

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

here’s the outcome:

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

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

Thanks again!