4
$\begingroup$

If I add a legend using

Legended[..., SwatchLegend[..., ...]]

then I can explicitly set the legend marker size using the LegendMarkerSize option. But I also need to explicitly set the style/colour of the legend elements a second time within the legend expression.

It is usually much more convenient to use the PlotLegens or the ChartLegends option. It spares me the trouble of having to manually match up the legens with the plot or chart. If I use this, can I still control the marker size?

Example:

cm = 72/2.54;
BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm]

How can I control the legend marker size in the above graphic? Is there anything simpler than an explicit legend specification as below?

colours = {Red, Blue, Black};
Legended[
 BarChart[{{1, 2, 3}, {1, 3, 2}}, ImageSize -> 4 cm, ChartStyle -> colours],
 SwatchLegend[colours, {"a", "b", "c"}, LegendMarkerSize -> 7]
]
$\endgroup$
8
$\begingroup$

I always do it like this:

cm = 72/2.54;
BarChart[{{1, 2, 3}, {1, 3, 2}}, 
  ChartLegends -> SwatchLegend[{"a", "b", "c"}, LegendMarkerSize -> 20], ImageSize -> 4 cm]

top: code from your question, bottom: this code

enter image description here

The colors of the legend are inherited from the BarChart object:

BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue}, 
 ChartLegends -> SwatchLegend[{"a", "b", "c"}, LegendMarkerSize -> 20], ImageSize -> 4 cm]

enter image description here

$\endgroup$
  • $\begingroup$ I added back the style specification to point out that it will be inherited by the legend (as this was my problem). Thanks! $\endgroup$ – Szabolcs Sep 7 '14 at 22:19
  • $\begingroup$ Could you please add back the style specification after your latest edit? Without it, it is not at all apparent that this solves the problem, as both the chart and the legending shows the default colours. $\endgroup$ – Szabolcs Sep 7 '14 at 22:20
  • $\begingroup$ Good point, I edited it and now it should be apparent that colors are inherited from chart and not just the same because default colors were used. $\endgroup$ – DaveStrider Sep 7 '14 at 22:27
8
$\begingroup$

For ChartLegends, although the documentation does not mention any values other than "Row" and "Column" for the option LegendAppearance, it turns out you can also use this option to set the LegendMarkerSize

BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue},
 LegendAppearance -> {LegendMarkerSize -> 20},
 ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm]

enter image description here

In fact, you can use any of the options for SwatchLegend as the setting for LegendAppearance:

Row[BarChart[{{1, 2, 3}, {1, 3, 2}}, ChartStyle -> {Red, Green, Blue},
             ChartLegends -> {"a", "b", "c"}, ImageSize -> 4 cm,
             LegendAppearance -> #] & /@ {
                         {LegendLabel -> "legend"},
                         {LabelStyle -> Red},
                         {LegendLayout -> "ReversedColumn", LegendMarkerSize -> 20},
                         {LegendLayout -> "ReversedRow", LegendMarkerSize -> 20, 
                              LegendMarkers -> "Bubble"}}]

enter image description here

For PlotLegends, this approach does not work.

$\endgroup$
  • $\begingroup$ I really did think I tried this, and it didn't work. But it does work (both in v9 and 10), and it looks to be the simplest solution. $\endgroup$ – Szabolcs Sep 8 '14 at 0:08
  • $\begingroup$ Great answer. Thank you for pointing out additional options for LegendAppearance. $\endgroup$ – nanjun Jan 25 '17 at 18:57

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.