Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

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

2 Answers 2

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], #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

share|improve this answer
    
+1 for a clean workaround. –  Jonie Jun 23 at 6:43
    
Thank you @Jonie. –  kguler Jun 23 at 7:47

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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