Common legend for subplots

With no bells and whistles.

CODE
using DataFrames, Random, StatsPlots; gr(dpi=600)
Random.seed!(8744)

function f(x, y)
    x ≤ .5 && y ≤ .5 ? 1 :
    x > .5 && y ≤ .5 ? 2 :
    x ≤ .5 && y > .5 ? 3 : 4
end

n = 4
df = [DataFrame(x =rand(100)*rand((1,0.5)), y=rand(100)*rand((1,0.5))) for _ in 1:n]
[transform!(df, [:x,:y]=> ByRow(f) => :quadrant) for df in df]

p = [@df d scatter(:x, :y, color=:quadrant, ms=3, msw=0.3, lims=(0, 1), ratio=1) for d in df]

colors = palette(:default)[1:n]'
labels = ["\n  Quadrant-1\n" "\n  Quadrant-2\n" "\n  Quadrant-3\n" "\n  Quadrant-4\n"]
l = @layout  [grid(2,2) a{0.2w}]
p0 = scatter((-n:-1)',(-n:-1)', lims=(0,1), legendfontsize=7, legend=:left,
        fg_color_legend = nothing, label=labels, fc=colors, frame=:none);
plot(p..., p0, layout=l, size=(800,500))
4 Likes