2
$\begingroup$

I need a combination of these two charts. I also cannot seem to get the labels "Above" the bars "Above" enough. They run into the error bars. When I added the counts (n = 1), the horizontal percentages no longer showed up. I do not know what else to try at this point. Any help would be appreciated.

This one is missing the percentage labels that are shown in the second picture, and the Above chart labels are not "Above" enough, they run into the error bars. I need a combination of the two charts.

This one does not have the other call outs, but the axis labeling is correct.  Need to add this to the first picture.

errorBar[type_: "Rectangle"][{{x0_, x1_}, {y0_, y1_}}, value_, 
  meta_] := Block[{error}, error = Flatten[meta];
  error = 
   If[error === {}, 0, 
    Last[error]]; {ChartElementData[type][{{x0, x1}, {y0, y1}}, value,
     meta], {Black, 
    Line[{{{(x0 + x1)/2, y1 - error}, {(x0 + x1)/2, 
        y1 + error}}, {{1/4 (3 x0 + x1), 
        y1 + error}, {1/4 (x0 + 3 x1), 
        y1 + error}}, {{1/4 (3 x0 + x1), 
        y1 - error}, {1/4 (x0 + 3 x1), y1 - error}}}]}}]

chartData = {14.77 -> 0.40832775506393815, 
   21.72 -> 0.12547384934195213, 20.22 -> 0.8224165242709678, 
   23.67 -> 0.3050610139336427, 20.88 -> 0.30377788713067355, 
   21.61 -> 0.9151942113259043, 18.23 -> 0.37645953006635097, 
   21.73 -> 0.3079566058897114, 26.33 -> 1.1410094954731147, 
   17.42 -> 0.4769719831680606, 18.86 -> 0.5250479766438282, 
   9.67 -> 0.7727768700660568, 11.05 -> 0.8229443075526639, 
   2.09 -> 0.24377502461544875};

labels = {"0%" , "0%" , "0.1%" , "0.1%" , "0.2%" , "0.2%", "1.0%" , 
   "1.0%" , "11.35%",  "2.5%" , "2.5%" , "5.0%" , "5.0%" , "10%"}; 

BarChart[chartData, ChartElementFunction -> errorBar["Rectangle"], 
 Frame -> True, GridLines -> Automatic, ImageSize -> 487.5, 
 ChartStyle -> {{Yellow, Orange, Blue, GrayLevel[0.30], Blue, 
    GrayLevel[0.30], Blue, GrayLevel[0.30], Red, Blue, 
    GrayLevel[0.30], Blue, GrayLevel[0.30], Blue}}, 
 ChartLegends -> 
  Placed[ SwatchLegend[{Yellow, Orange, Blue, GrayLevel[0.30], 
     Red}, {"\[OpenCurlyDoubleQuote]Processed polymer\
\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Measured \
polymer\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Randomly \
oriented\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Multi-line\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Single-line\[CloseCurlyDoubleQuote]"}], 
   Top], ChartLabels -> {Placed[labels, Axis, 
    Rotate[Style[#, FontFamily -> "Times"], Pi/2] &], 
   Placed[{"n = 1", "n = 1", "n = 1", "n = 1", "n = 1", "n = 1", 
     "n = 1", "n = 1", "n = 1", "n = 1", "n = 1", "n = 1", "n = 1", 
     "n = 1"}, Center]}, Frame -> Left, 
 FrameLabel -> {None, 
   Style["Ultimate tensile strength, \!\(\*SubscriptBox[\(\[Sigma]\), \
\(\(UT\)\(\\\ \)\)]\)[MPa]", FontFamily -> "Times"]}, 
 LabelingFunction -> (Placed[N[#/Total@1], Above] &)]

Has most of the elements that I need, but the n=#, needs to have the actual number, not just a consecutive count.

I'm still trying to get this to work. I can't get the actual count (n=#) to show up in my plot. They have actual values that are not consecutive as shown. I've tried adding them as a string, I've also tried adding them into the errorBarWithLabel function.

$\endgroup$
  • 2
    $\begingroup$ Can you post copyable code? The pictures are nice, but the code is important. $\endgroup$ – rcollyer Mar 10 '17 at 19:57
  • $\begingroup$ I tried to add code, but it won't copy correctly. Tried copying as input text, that also did not work. $\endgroup$ – Leora Homel Mar 10 '17 at 20:09
  • $\begingroup$ Ok. post it anyway, we'll see what we can do to fix it. $\endgroup$ – rcollyer Mar 10 '17 at 20:15
  • $\begingroup$ It exceeds the character limit. $\endgroup$ – Leora Homel Mar 10 '17 at 20:31
  • $\begingroup$ Hmm. What is needed then is a minimum working example, i.e. strip out anything that is not needed to demonstrate the problem. A brief look suggests that all of the options but ChartElementFunction, Frame, and LabelingFunction can be removed. Also, post a chartData with only 3 or 4 data points, just enough to demonstrate the issue, but not enough to overwhelm the system. $\endgroup$ – rcollyer Mar 10 '17 at 20:42
3
$\begingroup$

You can modify the function errorBar to add the bar labels above the error bars:

ClearAll[errorBarWithLabel]
errorBarWithLabel[type_: "Rectangle", shift_: .025, 
   style_: {12, "Panel"}][{{x0_, x1_}, {y0_, y1_}}, value_, meta_] := 
 Block[{error}, error = Flatten[meta]; error = If[error === {}, 0, Last[error]];
  {ChartElementData[type][{{x0, x1}, {y0, y1}}, value, meta], {Black, 
    Line[{{{(x0 + x1)/2, y1 - error}, {(x0 + x1)/2, y1 + error}}, 
         {{1/4 (3 x0 + x1), y1 + error}, {1/4 (x0 + 3 x1), y1 + error}}, 
         {{1/4 (3 x0 + x1), y1 - error}, {1/4 (x0 + 3 x1), y1 - error}}}], 
    Text[Style[value, ## & @@ style], {(x0 + x1)/2, y1 + error + shift}]}}]

Examples:

data = Flatten @ MapThread[{#1 -> #2} &, {RandomReal[1, 10], RandomReal[0.1, 10]}];
labels = StringJoin @@@ RandomChoice[CharacterRange["a", "z"], {10, 6}];
labelF = Placed[Row[{"n = ", #2[[2]]}, "  "], Center] &;

BarChart[data, LabelingFunction -> labelF, ImageSize -> 500, ChartStyle -> 1,
   ChartLabels -> {None, Placed[labels, Axis, Rotate[Style[#, 18, "Panel"], Pi/2] &]},
  ChartElementFunction -> errorBarWithLabel[]]

Mathematica graphics

Use the setting errorBarWithLabel["FadingRectangle", .025, {Bold, Italic, 12, Red, "Panel"}] to get:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Looks like this will probably work. I am going to implement and see if I can get it to go. Thank you for your help. $\endgroup$ – Leora Homel Mar 10 '17 at 22:33
  • $\begingroup$ @LeoraHomel, my pleasure. Welcome to mma.se. $\endgroup$ – kglr Mar 11 '17 at 9:58
  • $\begingroup$ I can't figure out how to change the n values to the actual count values that I need. I tried to modify the errorBarwithLabels function to include a third variable, but that didn't seem to work. $\endgroup$ – Leora Homel Mar 11 '17 at 18:56
  • $\begingroup$ Any thoughts @kglr? $\endgroup$ – Leora Homel Mar 11 '17 at 19:14
  • $\begingroup$ @Leora, in your original code the n values were hard-coded as 1 for each bar? In my code, (meant as a placeholder) they are the indices of the bars. Could you explain how actual count values are related to input data (chartData)? $\endgroup$ – kglr Mar 11 '17 at 23:32
1
$\begingroup$

I fixed the second issue by putting the count data into a variable, n, then calling it in the labelcount variable.

labelcount = Placed[Row[{"n=", n[#2[[2]]], ""}], Center] &;

BarChart[chartData, ChartElementFunction -> errorBarWithLabel[], 
 LabelingFunction -> labelcount, Frame -> True, 
 GridLines -> Automatic, ImageSize -> myImageSize, 
 LabelStyle -> myAxesStyle, 
 ChartStyle -> {{Yellow, Orange, Blue, GrayLevel[0.30], Blue, 
    GrayLevel[0.30], Blue, GrayLevel[0.30], Red, Blue, 
    GrayLevel[0.30], Blue, GrayLevel[0.30], Blue}}, 
 ChartLegends -> 
  Placed[ SwatchLegend[{Yellow, Orange, Blue, GrayLevel[0.30], 
     Red}, {"\[OpenCurlyDoubleQuote]Processed polymer\
\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Measured \
polymer\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Randomly \
oriented\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Multi\[Hyphen]line\
\[CloseCurlyDoubleQuote]", 
     "\[OpenCurlyDoubleQuote]Single\[Hyphen]line\
\[CloseCurlyDoubleQuote]"}], Top], 
 ChartLabels -> {Placed[labels, Axis, 
    Rotate[Style[#, FontFamily -> "Times"], Pi/2] &]}, Frame -> Left, 
 FrameLabel -> {None, 
   Style["Ultimate tensile strength, \!\(\*SubscriptBox[\(\[Sigma]\), \
\(\(UT\)\(\\\ \)\)]\)[MPa]", myScaledAxesFontSize, 
    FontFamily -> "Times"]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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