4
$\begingroup$

I want to put the line legend after the text, here is my code;

tn = .005;
lg = LineLegend[{Directive[Thick, Line, Black], 
   Directive[Thick, Black, Dashed], Directive[Thick, Red, Line], 
   Directive[Thick, Red, Dashed], 
   Directive[Thickness[tn], Line, Blue]}, {"Forward Pump", 
   "Backward Pump", Style["Forward Signal", Red], 
   Style["Backward Signal", Red], Style["ΔT", Blue]}, 
  Spacings -> {.1, 2}, LegendMarkerSize -> 50, 
  LabelStyle -> Directive[Bold, 15], LegendLayout -> "Row"]

The output isenter image description here

I want to make it like the following image,

The text should be before the line legend

Could you please let me know how I can put the text before the line legend?

$\endgroup$

3 Answers 3

7
$\begingroup$

Create your own layout function. For example:

myLayout[pairs_] := Grid[
    Partition[
        myRow /@ pairs,
        UpTo[3]
    ],
    Alignment -> Left
]

myRow[{legend_, label_}] := Row[{label, " ", legend}]

Then:

tn = .005;
lg = LineLegend[
    {Directive[Thick,Line,Black], Directive[Thick,Black,Dashed],Directive[Thick,Red,Line],Directive[Thick,Red,Dashed],Directive[Thickness[tn],Line,Blue]},
    {"Forward Pump","Backward Pump",Style["Forward Signal",Red],Style["Backward Signal",Red],Style["ΔT",Blue]},
    Spacings->{.1,2},
    LegendMarkerSize->50,
    LabelStyle->Directive[Bold,15],
    LegendLayout->myLayout
]

enter image description here

$\endgroup$
5
$\begingroup$

"MarkerLayout"

There is an (undocumented) option "MarkerLayout" to directly control the position of legend markers with respect to labels.

The usage is "MarkerLayout" -> pos where pos can be any of symbolic positions "Above","Below", "Before" and "After" (or Above, Below, Before and After).

So, you can add the option "MarkerLayout" -> "After" to get the desired result:

directives = {Directive[Thick, Line, Black], Directive[Thick, Black, Dashed], 
  Directive[Thick, Red, Line], Directive[Thick, Red, Dashed], 
  Directive[Thickness[tn], Line, Blue]}; 

labels = {"Forward Pump", "Backward Pump", Style["Forward Signal", Red], 
  Style["Backward Signal", Red], Style["ΔT", Blue]};

LineLegend[directives, labels, Spacings -> {.1, 2}, LegendMarkerSize -> 50, 
 LabelStyle -> Directive[Bold, 15], LegendLayout -> "Row", 
 "MarkerLayout" -> "After"]

enter image description here

LineLegend[directives, labels, Spacings -> 2, 
 LegendMarkerSize -> {50, 10}, LabelStyle -> Directive[Bold, 15], 
 LegendLayout -> {"Row", 1}, "MarkerLayout" -> "Above"]

enter image description here

Use "MarkerLayout" -> "Below" to get

enter image description here

The same option also works with SwatchLegend and PointLegend:

Row[SwatchLegend[{Red, Green, Blue}, {"red", "green", "blue"}, 
    LegendLayout -> "Column", "MarkerLayout" -> #] & /@ 
 {Before, After, Above, Below}, Spacer[20]]

enter image description here

Replace LegendLayout -> "Column" with LegendLayout -> "Row" to get

enter image description here

Row[PointLegend[{Red, Green, Blue}, {"red", "green", "blue"}, 
    LegendLayout -> "Column", Method -> "MarkerLayout" -> #] & /@
  {Before, After, Above, Below}, Spacer[20]]

enter image description here

Replace LegendLayout -> "Column" with LegendLayout -> "Row" to get

enter image description here

$\endgroup$
4
$\begingroup$

You can use the option LegendFunction with a custom function that reverses the ordering of labels and markers:

ClearAll[legendFunction]
legendFunction = Replace[#, 
           a : {PatternSequence[_Graphics, _] ..} :> 
                BlockMap[Apply[Sequence] @* Reverse, a, 2], 
     All] &;

Examples:

LineLegend[{Directive[Thick, Line, Black], 
    Directive[Thick, Black, Dashed], Directive[Thick, Red, Line], 
    Directive[Thick, Red, Dashed], 
    Directive[Thickness[tn], Line, Blue]}, {"Forward Pump", 
    "Backward Pump", Style["Forward Signal", Red], 
    Style["Backward Signal", Red], Style["ΔT", Blue]}, 
  Spacings -> {.1, 2}, LegendMarkerSize -> 50, 
  LabelStyle -> Directive[Bold, 15], LegendLayout -> "Row", 
  LegendFunction -> (Framed[legendFunction @ #] &)]

enter image description here

With Spacings -> {.5, 0, 3}, LegendLayout -> {"ReversedColumn", 2} and
LegendFunction ->(Panel[legendFunction@#, Background -> LightBlue]&) we get:

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.