2
$\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$
5
$\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$
2
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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