3
$\begingroup$

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]]]
$\endgroup$
3
  • 1
    $\begingroup$ You could build a GUI that allows you to select which items are shown in a legend via checkboxes but the solutions below are more straight forward for specific cases. $\endgroup$ Commented Jun 4, 2012 at 0:38
  • $\begingroup$ @MikeHoneychurch well, in principle I could, but in practice, that won't be me :) $\endgroup$
    – Verbeia
    Commented Jun 4, 2012 at 1:33
  • $\begingroup$ ...as per this video: ibnhconsulting.blogspot.com.au/2011/11/… $\endgroup$ Commented Jun 4, 2012 at 2:06

3 Answers 3

4
$\begingroup$

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]}]]
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Verbeia
    Commented Jun 4, 2012 at 0:00
6
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Definitely simpler. $\endgroup$
    – rcollyer
    Commented May 10, 2013 at 13:34

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.