# Common legend for subplots

**URL:** <https://discourse.julialang.org/t/common-legend-for-subplots/78079>\
**Category:** General Usage\
**Tags:** plotting, statsplots\
**Created:** [March 18, 2022, 11:46am UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079 "2022-03-18T11:46:19Z")\
**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:** [March 18, 2022, 11:46am UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079/1 "2022-03-18T11:46:19Z")

</div>

Hi all,

I have a follow up question to [this](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/2) ongoing issue about using a common legend across subplots. In my use case, a vector of plots with an unknown length are combined into a grid of subplots. Subplots do not necessarily have the same factors in the legend, but I need the legend key to be consistent. For example, “A” must be mapped to blue in all subplots even if some subplots do not contain “A”.

I am having trouble getting the hack in the previous post to work. The legend does not show up and there is an extraneous blank subplot. Is there a better solution?

Thanks!

**MWE**

```julia
using DataFrames, StatsPlots, Random 
Random.seed!(8744)

function f(x, y)
    if x ≤ .5 && y ≤ .5
        return 1
    elseif x > .5 && y ≤ .5
        return 2
    elseif x ≤ .5 && y > .5
        return 3
    else
        return 4
    end
end

df1 = DataFrame(x =rand(100), y=rand(100))
df2 = DataFrame(x =rand(100)*.5, y=rand(100))
transform!(df1, [:x,:y]=> ByRow(f) => :quadrant)
transform!(df2, [:x,:y]=> ByRow(f) => :quadrant)

p1 = @df df1 scatter(:x, :y, color=:quadrant, leg=false, ylims=(0, 1),xlims = (0,1))
p2 = @df df2 scatter(:x, :y, color=:quadrant, leg=false, ylims=(0, 1),xlims = (0,1))
plots = [p1,p2]
plot(plots..., plot(legend=true, framestyle=:none))

```

---

<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:** [March 19, 2022, 7:43pm UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079/2 "2022-03-19T19:43:49Z")

</div>

Would a single legend like this be helpful?

 ![Plots_gr_StatsPlots_unique_legend](https://global.discourse-cdn.com/julialang/original/3X/f/5/f5fc6e4d196b5cf3910a263c33817a99448839b0.jpeg)

---

<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:** [March 20, 2022, 3:26pm UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079/3 "2022-03-20T15:26:34Z")

</div>

Thanks for your reply! Yes. I am looking for something like that.

---

<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:** [March 20, 2022, 4:13pm UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079/4 "2022-03-20T16:13:47Z")

</div>

With no bells and whistles.

> **CODE**
>
> ```julia
> 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))
> 
> ```

---

<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:** [March 20, 2022, 7:30pm UTC](https://discourse.julialang.org/t/common-legend-for-subplots/78079/5 "2022-03-20T19:30:30Z")

</div>

Thank you!
