1
$\begingroup$

Im trying to make a legend with data labelled in groups. I'm doing this by combining SwatchLegends using Row, Column and Grid. Here is an example of a Row constructed legend:

   Framed[
 Row[
  {
   SwatchLegend[
    {Red, Green, Blue},
    {"label1", "label2", "label3"},
    LegendLabel -> "group1",
    LegendFunction -> "Frame"
    ],

   SwatchLegend[
    {Yellow, Purple},
    {"label4", "label5"},
    LegendLabel -> "group2", 
    LegendFunction -> "Frame"
    ],

   SwatchLegend[{Orange},
    {"label6"},
    LegendLabel -> "group3", 
    LegendFunction -> "Frame"
    ]
   },
  Alignment -> Top]
 ]

enter image description here

As you can see the option Alignment -> Top does not cause the legend objects to align at the top as I would expect. Interestingly, if I use Grid it works pretty much how I expect (with some extra spacing between the Grid elements):

Framed[
 Grid[
  {{SwatchLegend[{Red, Green, Blue},
     {"label1", "label2", "label3"},
     LegendLabel -> "group1",
     LegendFunction -> "Frame"],
    SwatchLegend[{Yellow, Purple},
     {"label4", "label5"},
     LegendLabel -> "group2", 
     LegendFunction -> "Frame"],
    SwatchLegend[{Orange},
     {"label6"},
     LegendLabel -> "group3",
      LegendFunction -> "Frame"]}},
  Alignment -> Top]]

enter image description here

Is this a bug? Am I using a wrong setting when using Row?

$\endgroup$
1
$\begingroup$

I usually use Multicolumn and it works as expected.

Framed[Multicolumn[{SwatchLegend[{Red, Green, Blue}, {"label1", 
     "label2", "label3"}, LegendLabel -> "group1", 
    LegendFunction -> "Frame"], 
   SwatchLegend[{Yellow, Purple}, {"label4", "label5"}, 
    LegendLabel -> "group2", LegendFunction -> "Frame"], 
   SwatchLegend[{Orange}, {"label6"}, LegendLabel -> "group3", 
    LegendFunction -> "Frame"]}, 3, Alignment -> Top]]

enter image description here

or ddd vertical and horizontal space with Spacings -> {0, 0}

Framed[Multicolumn[{SwatchLegend[{Red, Green, Blue}, {"label1", 
     "label2", "label3"}, LegendLabel -> "group1", 
    LegendFunction -> "Frame"], 
   SwatchLegend[{Yellow, Purple}, {"label4", "label5"}, 
    LegendLabel -> "group2", LegendFunction -> "Frame"], 
   SwatchLegend[{Orange}, {"label6"}, LegendLabel -> "group3", 
    LegendFunction -> "Frame"]}, 3, Alignment -> Top, 
  Spacings -> {0, 0}]]

enter image description here

$\endgroup$
1
$\begingroup$

Update: An alternative approach is to replace "Frame" with a custom LegendFunction:

ClearAll[legendFunc]
legendFunc[pos_: (Top -> Top)] := Framed[#, FrameStyle -> Gray, 
    RoundingRadius -> 5, FrameMargins -> 0, BaselinePosition -> pos] &

Framed[Row[{SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"},
     LegendLabel -> "group1", LegendFunction -> legendFunc[]], 
   SwatchLegend[{Yellow, Purple}, {"label4", "label5"}, 
    LegendLabel -> "group2", LegendFunction -> legendFunc[]], 
   SwatchLegend[{Orange}, {"label6"}, LegendLabel -> "group3", 
    LegendFunction -> legendFunc[]]}, Spacer[1]]] 

enter image description here

Original answer:

Grid and Multicolumn are my preferred solutions. However, if you have to use Row you can wrap each element with Pane and use the option BaselinePosition -> Top -> Top:

ClearAll[baselinePos]
baselinePos[pos_: (Top -> Top)] := Pane[#, "BaselinePosition" -> pos] &;

Framed[Row[baselinePos[] /@ 
   {SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, LegendLabel -> "group1",
      LegendFunction -> "Frame"], 
    SwatchLegend[{Yellow, Purple}, {"label4", "label5"}, LegendLabel -> "group2",
      LegendFunction -> "Frame"], 
    SwatchLegend[{Orange}, {"label6"}, LegendLabel -> "group3", 
      LegendFunction -> "Frame"]}]]

enter image description here

Use Spacer[1] as the second argument in Row to get

enter image description here

Use baselinePos[Bottom -> Bottom] to 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.