2
$\begingroup$

I would like to add 4 legends on a simple bar chart below, but only the last 2 legends appear on the graph.

BarChart[{{1, 2, 3, 4}, {3, 1, 2, 5}, {5, 2, 3, 2}, {2, 3, 1, 3}}, 
ChartLegends -> {Placed[SwatchLegend[{Black}, {"Show"}], {0.2, 0.2}],
Placed[SwatchLegend[{Red}, {"me"}], {0.4, 0.4}],
Placed[SwatchLegend[{Pink}, {"Hello"}], {0.8, 0.8}],
Placed[SwatchLegend[{Green}, {"World"}], {0.9, 0.9}]}]

Above is the break down version, initial command is like this, neither one works:

BarChart[{{1, 2, 3, 4}, {3, 1, 2, 5}, {5, 2, 3, 2}, {2, 3, 1, 3}},
ChartLegends -> MapThread[Placed[SwatchLegend[{#1}, {#2}], #3] &,
{{Black, Red, Pink, Green}, {"Show", "me", "Hello","World"}, 
{{0.2, 0.2}, {0.4, 0.4}, {0.8, 0.8}, {0.9, 0.9}}}]]

Although I can group them up and call it liks below, but the legends need to be placed as specified.

BarChart[{{1, 2, 3, 4}, {3, 1, 2, 5}, {5, 2, 3, 2}, {2, 3, 1, 3}}, 
ChartLegends -> {Placed[SwatchLegend[{Black, Red, Pink, Green},
{"Show", "me", "Hello", "World"}], {0.2, 0.2}]}]

PlotLegends don't seem to have such issue:

ListPlot[{{1, 2, 3, 4}, {3, 1, 2, 5}, {5, 2, 3, 2}, {2, 3, 1, 3}}, 
PlotLegends -> MapThread[Placed[SwatchLegend[{#1}, {#2}], #3] &,
{{Black, Red, Pink, Green}, {"Show", "me", "Hello", "World"}, 
{{0.2, 0.2}, {0.4, 0.4}, {0.8, 0.8}, {0.9, 0.9}}}]]
$\endgroup$

1 Answer 1

2
$\begingroup$

ChartLegends and PlotLegends have subtle differences, so it is not quite comparing two equivalent things. In this case, though, I think using either in this case is incorrect as you are creating your own legends, completely. I would use Legended directly, e.g.

Legended[
 BarChart[{{1, 2, 3, 4}, {3, 1, 2, 5}, {5, 2, 3, 2}, {2, 3, 1, 3}}],
 MapThread[Placed[SwatchLegend[{#1}, {#2}], #3] &,
  {{Black, Red, Pink, Green}, {"Show", "me", "Hello","World"}, 
  {{0.2, 0.2}, {0.4, 0.4}, {0.8, 0.8}, {0.9, 0.9}}}]
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, @rcollyer ! This seems to be the solution, now I have to see how to incorporate this into my actual codes... $\endgroup$
    – Leonx
    Commented Oct 29, 2014 at 6:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.