6

I am wondering if one can place on the right of the plot frame a single legend that is a combination of a LineLegend and a SwatchLegend.

What I want is to see the line with its label on top of the swatch with its label as if the swatch was the second item of a LineLegend.

I have tried

 PlotLegends -> {LineLegend[{Blue}, {"first item"}, 
     LabelStyle -> {Bold, 20}], 
     SwatchLegend[{Opacity[0.3, Blue]}, {"second item"}]}

and some variations with Join[] but I have not succeded to put all these legends on the right of the frame. For instance the code above places one legend on the right and one at the bottom, which is not what I want.

Is this merge possible or I should write something from scratch?

Thanks for your inputs, Roberto

Rho Phi
  • 1,420
  • 11
  • 21

3 Answers3

7

Additional alternatives to mix different legends:

lg1 = SwatchLegend[{Red}, {"sin"}];
lg2 = LineLegend[{Blue}, {"cos"}];
lg3 = PointLegend[{Green}, {"sinc"},   LegendMarkers -> "\[FilledCircle]"];

Plot[{Sin[x], Cos[x], 2 Sinc[x]}, {x, -2 Pi, 2 Pi},
     PlotStyle -> Thread[{Thick, {Red, Blue, Green}}], ImageSize -> 400,
     PlotLegends -> (Placed[#, Right] & /@ {lg1, lg2, lg3})]

enter image description here

or PlotLegends -> {{lg1, Right}, {lg2, Right}, {lg3, Right}}

or PlotLegends -> Column[{lg1, lg2, lg3}]] (as in Roberto's answer)

And a few alternative ways to use Legended in addition to the method in ubpdqn's answer:

Legended[Plot[{Sin[x], Cos[x], 2 Sinc[x]}, {x, -2 Pi, 2 Pi},
   PlotStyle -> Thread[{Thick, {Red, Blue, Green}}], ImageSize -> 400], 
   (Placed[#, Right] & /@ {lg1, lg2, lg3})]

Alternatively, use Column[{lg1, lg2, lg3}]] or {{lg1, Right}, {lg2, Right}, {lg3, Right}} in the second argument of Legended.

kglr
  • 394,356
  • 18
  • 477
  • 896
4

Perhaps this contrived example will be helpful:

lleg = LineLegend[{Red}, {"sin"}];
sleg = SwatchLegend[{Blue}, {"cos"}];
Legended[Show[Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Red], 
  ListPlot[Table[{j, Cos[j]}, {j, 0 , 2 Pi, 0.11}]]], 
 Framed[Column[{lleg, sleg}], RoundingRadius -> 10, 
  Background -> LightBlue]]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
1

The legends need to go into a Column[] ...

 PlotLegends -> 
  Column[{LineLegend[{Blue}, {"First"},], 
   SwatchLegend[{Opacity[0.3, Blue]}, {"Second"}, 
   ]}]
Rho Phi
  • 1,420
  • 11
  • 21