2
$\begingroup$

I have a stacked bar chart and one of the bars in the chart shows the value. I am using Labeled to put the value where I need it, but I can't seem to figure out how to change the color of the text. I have tried using Style wrapped around the text, using LabelStyle->White within Labeled, and using LabelStyle with Directive, but nothing seems to affect the color of my text, which is hard to see on the blue bar. Here's my code:

myPercentPrecision[x_] := 
 SetPrecision[Interpreter["PercentFraction"][x], 4]
BarChart[{{Labeled[myPercentPrecision[.5833], 
    myPercentPrecision[.5833], Center, Bold, 18], 
   myPercentPrecision[1 - .5833]}, {Labeled[myPercentPrecision[.7519],
     myPercentPrecision[.7519], Center, Bold, 18], 
   myPercentPrecision[1 - .7519]}}, ChartLayout -> "Stacked", 
 ChartStyle -> {EdgeForm[], {LLCCBlue, LightGray}}, BarSpacing -> 1.5,
  ChartLabels -> {Placed[{Style["RDG-098\n", Bold, 22], 
     Style["RDG-099\n", Bold, 22]}, Above], None}, 
 ChartLegends -> 
  Placed[{Style["Successful", 18], Style["Unsuccessful", 18]}, Right]]

And the resulting output: stacked bar chart

The bar chart is inside a Manipulate function, but I've also tried building it outside of Manipulate, and I'm having the same issue with the label color.

$\endgroup$
4
  • 1
    $\begingroup$ The code does not work on my machine. Could you provide a minimal and complete example? (myPercentPrecision etc. are undefined, try providing example data) $\endgroup$
    – Gladaed
    Nov 13, 2018 at 15:49
  • $\begingroup$ Just add the color you want the label to be after you specify the weight and size, BarChart[{ {Labeled[0.5, "White", Center, Bold, 18, White], 0.5}, {Labeled[0.25, "Green", Center, Bold, 18, Green], 0.25}}, ChartLayout -> "Stacked", ChartStyle -> {EdgeForm[], {Red, LightGray}}, BarSpacing -> 1.5] If this doesn't work, you might need to specify what version of MMA you have or post more complete code. $\endgroup$
    – N.J.Evans
    Nov 13, 2018 at 16:21
  • $\begingroup$ Updated post to provide a minimum working example. $\endgroup$
    – kgrafton
    Nov 13, 2018 at 16:42
  • $\begingroup$ @N.J.Evans I tried that and it didn't work. I'm using version 12 prerelease, although I tested in version 11.3 and had the same issue. $\endgroup$
    – kgrafton
    Nov 13, 2018 at 16:43

2 Answers 2

1
$\begingroup$
myPercentPrecision[x_] := 
 SetPrecision[Interpreter["PercentFraction"][x], 4]
BarChart[
 {
  {Labeled[myPercentPrecision[.5833], myPercentPrecision[.5833], 
    Center, Bold, 18, White], 
   myPercentPrecision[1 - .5833]}, {Labeled[myPercentPrecision[.7519],
     myPercentPrecision[.7519], Center, Bold, 18, White], 
   myPercentPrecision[1 - .7519]}
  }
 , ChartLayout -> "Stacked"
 , ChartStyle -> {EdgeForm[], {Red, LightGray}}
 , BarSpacing -> 1.5
 , ChartLabels -> {Placed[{Style["RDG-098\n", Bold, 22], 
     Style["RDG-099\n", Bold, 22]}, Above], None}
 , ChartLegends -> 
  Placed[{Style["Successful", 18], Style["Unsuccessful", 18]}, Right]
 ]

Using your code, and replacing LLCCBlue with Red and adding White as the last argument to Labeled, I produce the following image:

enter image description here

Note: I am using v. 11.2 if your results are different, there may be a bug.

$\endgroup$
1
  • 1
    $\begingroup$ That's one of the things I've tried. Just tried it again, even in a fresh notebook, and no luck. I'll contact their support and see if it's a bug with 12. $\endgroup$
    – kgrafton
    Nov 13, 2018 at 21:13
0
$\begingroup$
myPercentPrecision[x_] := SetPrecision[Interpreter["PercentFraction"][x], 4] BarChart[{{Labeled[myPercentPrecision[.5833], myPercentPrecision[.5833], Center, Bold, 18, Yellow], myPercentPrecision[1 - .5833]}, {Labeled[myPercentPrecision[.7519], myPercentPrecision[.7519], Center, Bold, 18, Green], myPercentPrecision[1 - .7519]}}, ChartLayout -> "Stacked", ChartStyle -> {EdgeForm[], {Red, LightGray}}, BarSpacing -> 1.5, ChartLabels -> {Placed[{Style["RDG-098\n", Bold, 22, Yellow], Style["RDG-099\n", Bold, 22, Red]}, Above], None}, ChartLegends -> Placed[{Style["Successful", 18, Green], 
Style["Unsuccessful", 18, Blue]}, Right]]    

I am using M11.1, and it is just working fine. Which text exactly that could not change?

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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