# Frame around elements of swatch legend

I'd like to put a frame around the elements of swatch legend:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"},
LegendLayout -> "Row"]


That is, not around the entire legend like this:

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


but ONLY around the red, green and blue squares (a separate frame around each square). Is that possible?

• Around all the squares together, or around each square individually? – Mr.Wizard Jul 31 '17 at 12:46
• @Mr.Wizard: Around each square separately. Sorry, it didn't became clear I corrected it – holistic Jul 31 '17 at 13:05

One method is to use LegendMarkers and draw a frame using an empty Rectangle:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"},
LegendFunction -> "Frame", LegendMarkerSize -> 30,
LegendMarkers ->
Graphics[{Rectangle[], FaceForm[], EdgeForm[Opacity[1]], Rectangle[{-1/2, -1/2}, {3/2, 3/2}]}]]


Another is to use LegendFunction to operate on the contents of the legend, here with MapAt and a rather unwieldy position specification:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"},
LegendFunction -> MapAt[Framed, {1, 1, 1, 1, 1, 1, 1, All, 1}]]


From a comment below you may want EdgeForm rather than a separate frame element:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"},
LegendFunction -> "Frame", LegendMarkerSize -> 20,
LegendMarkers -> Graphics[{EdgeForm[{Thick, Black, Opacity[1]}], Rectangle[]}]]


• Ah I see,thank you! So the markers were just replaced by rectangles, interesting solution. Is it possible to remove the space between the colored rectangles and the "frame" rectangle and color the frame Black? I tried: Graphics[{Rectangle[], FaceForm[], Black, Rectangle[{-1/2, -1/2}, {3/2, 3/2}]}] but that didn't seem to work – holistic Jul 31 '17 at 13:18
• @holistic It sounds like you might want EdgeForm rather than a separate frame. I'll add an example of that in a moment. – Mr.Wizard Jul 31 '17 at 13:23
• Thanks so much for those many examples, could learn a lot :). Yes, EdgeForm works very well, did not know about this yet! – holistic Jul 31 '17 at 13:57