RE: Legend on BarChart
- To: mathgroup at smc.vnet.net
- Subject: [mg81224] RE: [mg81185] Legend on BarChart
- From: "David Annetts" <davidannetts at aapt.net.au>
- Date: Sun, 16 Sep 2007 04:07:29 -0400 (EDT)
- References: <200709150806.EAA27996@smc.vnet.net>
Hi Tom,
> I'm using version 6 and would like to add a legend to a
> BarChart. I found the below example on this site. It works
> but produces a raft of errors :
Not surprised -- both packages are legacy packages in 6.x ....
>
> << Graphics`Legend`
> << Graphics`Graphics`
> ShowLegend[
> BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4,
> 3}], {{{Hue[0],
> StyleForm["First", FontFamily -> "Times", FontSize -> 12,
> FontWeight -> "Bold"]}, {Hue[0.6],
> StyleForm["Second", FontFamily -> "Times", FontSize -> 12,
> FontWeight -> "Bold"]}}, LegendPosition -> {1.1, -0.5}}]
>
> Is there a better method to add a Legend to a BarChart in version 6 ?
That depends on what you mean by better ....
For me, it was easier to use appropriately-coloured text in an Epilog->{}
than to stuff around with the legend package.
For your example, we want to load PlotLegends & BarCharts, then
Needs["PlotLegends`"]
Needs["Histograms`"]
ShowLegend[
BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4,
3}], {{{Hue[0],
StyleForm["First", FontFamily -> "Times", FontSize -> 12,
FontWeight -> "Bold"]}, {Hue[0.6],
StyleForm["Second", FontFamily -> "Times", FontSize -> 12,
FontWeight -> "Bold"]}}, LegendPosition -> {1.1, -0.5}}]
Produces a graph with a legend, but it's probably not what you were after.
You could remedy this using BarStyle
ShowLegend[
BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4, 3},
BarStyle -> {Hue[0], Hue[0.6]}], {{{Hue[0],
StyleForm["First", FontFamily -> "Times", FontSize -> 12,
FontWeight -> "Bold"]}, {Hue[0.6],
StyleForm["Second", FontFamily -> "Times", FontSize -> 12,
FontWeight -> "Bold"]}}, LegendPosition -> {1.1, -0.5}}]
Now, only the legend looks disproportionately large, especially if you add a
title. We can fix this by reducing the font size & the shadow.
ShowLegend[
BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4, 3},
BarStyle -> {Hue[0], Hue[0.6]},
PlotLabel ->
"Bar chart with legend"], {{{Hue[0],
StyleForm["First", FontFamily -> "Times", FontSize -> 10,
FontWeight -> "Bold"]}, {Hue[0.6],
StyleForm["Second", FontFamily -> "Times", FontSize -> 10,
FontWeight -> "Bold"]}}, LegendPosition -> {.8, -.54},
LegendShadow -> .001}]
An alternative is to forget Legend & use an Epilog as in
border = Line[{{5.5, -.5}, {6.75, -.5}, {6.75, -2.5}, {5.5, -2.5}, \
{5.5, -.5}}]; BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4, 3},
BarStyle -> {Hue[0], Hue[0.6]},
PlotLabel -> "Bar chart with legend",
Epilog -> {
Style[Text["First", {6.5, -1}, {1, 0}], 12, Hue[0], Bold],
Style[Text["Second", {6.5, -2}, {1, 0}], 12, Hue[.6], Bold],
border}]
Regards,
Dave.
- References:
- Legend on BarChart
- From: tomfabtastic@hotmail.com
- Legend on BarChart