2
$\begingroup$

Been fiddling around with the legends and trying to place one in each corner and I bumped into an issue:

BarChart[{{12, 14, 16, 18}, {8, 10, 11, 12}, {2, 5, 6, 7}, {1, 3, 5, 7}}, 
  ChartStyle -> {Green, Blue, Red, Orange}, ChartLegends ->     
  MapThread[Placed[PointLegend[{#1},{TextCell[#2,TextAlignment->Left]},
  LegendMarkers->{#3},LegendMarkerSize->20,LabelStyle->{FontFamily->"Arial",18}
  ,Spacings->{0.2,0.2}],#4]&,{{Green, Blue, Red, Orange}, 
  {"A","B","D","C"},{{"\[FilledSquare]",22},{"\[FilledCircle]",22},{"\[FilledUpTriangle]",22}
  ,{"\[FilledDiamond]",22}},{{0.8,0.1},{0.1,0.1},
  {0.1,0.8},{0.8,0.8}}}]]

Result

which doesn't show all 4 legends. But this works for PlotLegends as below

ListPlot[{{{0.2, 0.4}, {0.4, 0.8}}, {{0.1, 0.5}, {0.3, 0.7}}, {{0.4, 0.7}, 
  {0.7, 1.1}}, {{0.1, 0.7}, {0.8, 0.5}}}, Joined -> True, PlotStyle-> {Green, Blue, Red, Orange},
  PlotMarkers -> {\[FilledSquare], \[FilledCircle], \[FilledUpTriangle], \[FilledCircle]}
  , PlotLegends -> MapThread[Placed[PointLegend[{#1},
  {TextCell[#2,TextAlignment->Left]},LegendMarkers->{#3},LegendMarkerSize->20,
  LabelStyle->{FontFamily->"Arial",18},Spacings->{0.2,0.2}],#4]&,{{Green, 
  Blue, Red, Orange},{"A","B","D","C"},{{"\[FilledSquare]",22},{"\
  [FilledCircle]",22},{"\[FilledUpTriangle]",22},{"\[FilledDiamond]",22}},
  {{0.8,0.1},{0.1,0.1},{0.1,0.8},{0.8,0.8}}}]]

enter image description here

A workaround possibly is using Epilog to inset the legends into their positions. But I was wondering if anyone knew why this did not work for ChartLegends?

Edit: Added image for ListPlot, and fixed the syntax.

$\endgroup$
  • $\begingroup$ your BarChart example doesn't work at all (non-matching brackets etc.) $\endgroup$ – eldo Jun 23 '14 at 2:04
  • $\begingroup$ Thanks for pointing that out eldo, I've fixed the code up. There were unnecessary spaces and new lines in the previous. $\endgroup$ – Jonie Jun 23 '14 at 3:45
4
$\begingroup$

A work-around: wrap the BarChart with Legended

Legended[BarChart[{{12, 14, 16, 18}, {8, 10, 11, 12}, {2, 5, 6, 7}, {1, 3, 5, 7}},
   ChartStyle -> {Green, Blue, Red, Orange}, ImageSize -> 500],
 MapThread[Placed[PointLegend[{#1}, {#2}, LegendMarkers -> {#3},
   LegendMarkerSize -> 20, LabelStyle -> {FontFamily -> "Arial", 18}, 
   Spacings -> {0.2, 0.2},
   LegendFunction -> (Framed[#, Background -> Opacity[.5, Yellow]] &)], #4] &,
  {{Green, Blue, Red, Orange},
   {"A", "B", "D", "C"},
   {{"\[FilledSquare]", 22}, {"\[FilledCircle]", 22}, 
    {"\[FilledUpTriangle]", 22}, {"\[FilledDiamond]", 22}},
   {{0.8, 0.1}, {0.1, 0.1}, {0.1, 0.8}, {0.8, 0.8}}}]]

enter image description here

$\endgroup$
1
$\begingroup$

This is a "cheat", if I interpret the desired visualization correctly.

bc = BarChart[{{12, 14, 16, 18}, {8, 10, 11, 12}, {2, 5, 6, 7}, {1, 3,
     5, 7}}, ChartStyle -> {Green, Blue, Red, Orange}]
p = Plot[, {x, 0, 5}, PlotRange -> {0, 20}, 
   PlotLegends -> 
    MapThread[
     Placed[PointLegend[{#1}, {TextCell[
          Style[#2, FontColor -> Purple, FontFamily -> "Arial", 18], 
          TextAlignment -> Left, Background -> Yellow]}, 
        LegendMarkers -> {#3}, LegendMarkerSize -> 20, 
        Spacings -> {0.2, 0.2}], #4] &, {{Green, Blue, Red, 
       Orange}, {"A", "B", "D", 
       "C"}, {{"\[FilledSquare]", 22}, {"\[FilledCircle]", 
        22}, {"\[FilledUpTriangle]", 22}, {"\[FilledDiamond]", 
        22}}, {{0.8, 0.1}, {0.1, 0.1}, {0.1, 0.8}, {0.8, 0.8}}}]];
Show[bc, p]

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.