3
$\begingroup$

I have created a SwatchLegend called mySwatchLegend. I have set LegendLayout to "Row" and LegendFunction to "Frame":

myLabelStyle = {FontFamily -> "Arial", 24};
mySwatchLegend = SwatchLegend[
  {Red, Yellow,
   Green, Blue, Purple},
  {"series 1a", "series 1b",
   "series 2a", "series 2b", "series 2c"},
  LegendFunction -> "Frame",
  LegendLayout -> "Row",
  LegendMarkerSize -> 16,
  LabelStyle -> myLabelStyle
  ]

mySwatchLegend

I would like the last three legend items -- series 2a, series 2b, and series 2c -- to all be on the second line/row of the legend, instead of having series 2a dangling on the end of the legend's first line/row as it is now. How can I accomplish this?

$\endgroup$

2 Answers 2

8
$\begingroup$
myLabelStyle = {FontFamily -> "Arial", 24};
mySwatchLegend = 
 SwatchLegend[
  {Red, Yellow, Green, Blue, Purple},
  {"series 1a", "series 1b", "series 2a", "series 2b", "series 2c"}, 
  LegendFunction -> "Frame", 
  LegendLayout -> (Grid[Map[Splice, TakeDrop[#, 2], {2}]] &), 
  LegendMarkerSize -> 16, LabelStyle -> myLabelStyle]

legend

$\endgroup$
2
$\begingroup$

Use Insert[#, {}, m] for some position m to insert empty placeholder.

Use Partition[(*...*), UpTo[n]] where n represents number of columns of labels.

myLabelStyle = {FontFamily -> "Arial", 24};
mySwatchLegend = 
 SwatchLegend[{Red, Yellow, Green, Blue, Purple}, {"Label 1", 
   "Label 2", "Label 3", "Label 4", "Label 5"}, 
  LegendFunction -> "Frame", 
  LegendLayout -> (Grid[
      Partition[Grid[#] & /@ List /@ Insert[#, {}, 3], UpTo[3]], 
      Spacings -> {1, Automatic}] &), LegendMarkerSize -> 16, 
  LabelStyle -> myLabelStyle]

enter image description here

Table[With[{m = n}, 
  SwatchLegend[{Red, Yellow, Green, Blue, Purple}, {"Label 1", 
    "Label 2", "Label 3", "Label 4", "Label 5"}, 
   LegendFunction -> "Frame", 
   LegendLayout -> (Grid[
       Partition[Grid[#] & /@ List /@ Insert[#, {}, m], UpTo[3]], 
       Spacings -> {1, Automatic}] &), LegendMarkerSize -> 16, 
   LabelStyle -> myLabelStyle]], {n, 6}]

enter image description here

For two columns it could be:

myLabelStyle = {FontFamily -> "Arial", 24};
mySwatchLegend = 
 SwatchLegend[{Red, Yellow, Green, Blue, Purple}, {"Label 1", 
   "Label 2", "Label 3", "Label 4", "Label 5"}, 
  LegendFunction -> "Frame", 
  LegendLayout -> (Grid[
      Partition[Grid[#] & /@ List /@ Insert[#, {}, 2], UpTo[2]], 
      Spacings -> {1, Automatic}] &), LegendMarkerSize -> 16, 
  LabelStyle -> myLabelStyle]

enter image description here

For four columns several empty placeholders would be needed to be inserted.

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.