7

I have Legends that stretch over multi-lines and the only way I can get them left aligned is by adding white space to the end of the legend item. Does anyone know if there is a way to use TextAlignment with SwatchLegend?

gJ = Plot[Sin[x], {x, 1, 12}, 
  PlotLegends -> {Placed[
     SwatchLegend[{RGBColor[1, 0.5, 0], RGBColor[0., 0.329412, 0.], 
       RGBColor[0.619608, 0.25098, 1.], 
       RGBColor[0, 1, 1]}, {"it ends up centred", "but ", 
       "fox jumped over two lines that I want to \nalign to the left",
        "A quick brown"}, 
      LegendMarkers -> {Graphics[{EdgeForm[], Rectangle[]}], 
        Graphics[{EdgeForm[], Rectangle[]}], 
        Graphics[{EdgeForm[], Rectangle[]}], 
        Graphics[{EdgeForm[], Rectangle[]}], 
        Graphics[{EdgeForm[], Rectangle[]}]}, 
      LegendLayout -> "ReversedColumn", LegendMarkerSize -> 16, 
      LabelStyle -> {FontFamily -> "Arial", 18}, 
      Spacings -> {0.5, 0.25}], {0.4167, 0.7701}]}]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
WolframFan
  • 1,412
  • 14
  • 21

2 Answers2

5

The fastest way is to use TextCell only there you can have full control over the text:

lbl = TextCell["fox jumped over two lines that I want to\nalign to the left", 
         TextAlignment -> Left];

So:

Plot[Sin[x], {x, 1, 12}, ImageSize -> 500, Frame -> True, Axes -> False,
             PlotLegends -> {Placed[SwatchLegend[{Red}, {lbl}, 
                                     LabelStyle -> {FontFamily -> "Arial", 18},
                                     LegendMarkerSize -> 16], 
                             {0.4167, 0.7701}]}]

enter image description here

Related usage of TextJustification on StarWars theme: 47273

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Since I am programmatically generating the Legend command with a GUI your answer will be the easiest way. Thanks. – WolframFan May 14 '14 at 06:24
  • 1
    @WolframFan I'm glad you find it useful. It looks simple but it took me a lot of health to realise that painless text formatting without TextCell is impossible :P – Kuba May 14 '14 at 06:27
  • Would Pane work as well? @WolframFan -this should probably be automated in the underlying legend-creating code if possible. – Verbeia May 14 '14 at 10:44
  • @Verbeia Yes, this can be done multiple ways. But I wanted to advertise TextCell since you can't use e.g. TextJustification without this. So we can use TextCell to text formatting or we can use shorter ways but not general. – Kuba May 14 '14 at 10:47
  • @Kuba of course, but isn't Alignment and Pane equivalent to this? – Verbeia May 14 '14 at 11:20
  • @Verbeia Let's go to chat :) – Kuba May 14 '14 at 11:26
5

Adapting an example in the docs you could do it this way:

table[pairs_] := Grid[pairs, BaseStyle -> {TextAlignment -> Left}, Alignment -> 
{Left, Automatic}]

Then use LegendLayout -> table, to give you:

enter image description here

Edit

To reverse it just use Reverse:

reversetable[pairs_] := Grid[Reverse@pairs, BaseStyle -> {TextAlignment -> Left},    
Alignment -> {Left, Automatic}]

LegendLayout -> reversetabletable,

enter image description here

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158