3

Further to my previous question, is there a concise way - amenable to automated setting in a package so that it can be accessed globally - to eliminate some legend item that would otherwise show? For example, in this example, I'd like to only show the orange rectangle and text "Preliminary estimates".

enter image description here

I tried a couple of replacement rules, by analogy with belisarius’s answer for reversing stacked bar chart legends, such as the example below, but it doesn’t seem to make any difference.

BarChart[RandomVariate[NormalDistribution[0, 0.6], 40], 
 ChartStyle -> Join[ConstantArray[Green, {39}], {Orange}], 
 ChartLegends -> 
 Placed[Join[ConstantArray[None, {39}], {"Preliminary estimates"}], 
 Bottom]] /. Pane[Row[x_]] :> Pane[Row[Last[x]]]
Verbeia
  • 34,233
  • 9
  • 109
  • 224

3 Answers3

6

Use MapAt to apply the Legended and Style wrappers directly to the last item in the dataset:

data = RandomVariate[NormalDistribution[0, 0.6], 40];

data = MapAt[Legended[Style[#, Orange], "Preliminary estimate"] &, data, -1];

BarChart[data, ChartStyle -> Green, LegendAppearance -> "Row"]

enter image description here

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
4

The reason your code doesn't work is because the legends are typeset using a Row with two arguments (where the second argument is a separator which should be placed between the entries in the legend) but you're matching to a Row with only one argument. You could instead do something like

BarChart[RandomVariate[NormalDistribution[0, 0.6], 40], 
 ChartStyle -> Join[ConstantArray[Green, {39}], {Orange}], 
 ChartLegends -> 
 Placed[Join[ConstantArray[None, {39}], {"Preliminary estimates"}], 
 Bottom]] /. Pane[Row[x_, ___]] :> Pane[Last[x]]

Mathematica graphics

Edit

Another way to selectively label entries in the BarChart is to do something like this

BarChart[RandomVariate[NormalDistribution[0, 0.6], 40], 
 ChartStyle -> Join[ConstantArray[Green, {39}], {Orange}], 
 ChartLegends -> 
  Join[ConstantArray[
    None, {39}], {Placed["Preliminary estimates", Bottom]}]]
Heike
  • 35,858
  • 3
  • 108
  • 157
  • I just realised that some months ago I had come up with the second option you posted here and forgotten about it! It's definitely the cleanest option for my purpose. – Verbeia Jun 04 '12 at 00:00
3

Of course, this got a lot easier in version 9:

BarChart[RandomVariate[NormalDistribution[0, 0.6], 40], 
 ChartStyle -> Join[ConstantArray[Green, {39}], {Orange}], 
 ChartLegends -> Placed[SwatchLegend[{Orange}, {"Estimate"}], Bottom]]

enter image description here

Verbeia
  • 34,233
  • 9
  • 109
  • 224