Hi all,
I have a follow up question to this 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
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))