3
$\begingroup$

I am trying to create a pareto chart for some non technical folks and having the hardest time formatting it correctly…

Here’s my sample code:

(*Some dynamically generated data *)

mydata = {0.9436, 2.20725333345, 2.1402, 1.8729, 4.9436, 0.4819};
mylabels = {"l1", "l2", "l3", "l4", "some really long label", "another really long label", "l7"};

(*lets riffle and sort the data *)

a =  Sort[ Partition[Riffle[mylabels, mydata], 2], #1[[2]] > #2[[2]] & ];

BarChart[a , BarOrigin -> Left, BarSpacing -> -1, LabelingFunction -> (Placed[#, After] &), Axes -> None, Frame -> True, FrameTicks -> None, FrameLabel -> "Some Title", GridLines -> None, AspectRatio -> 0.3, ImageSize -> Full, ChartStyle -> "Pastel"]

A few questions:

  • What is the best way to truncate to 2 digits in the labels, I tried using NumberForm in the Sort function but doesn’t work since Numberform is a wrapper.

  • Labeling works reasonably well when labels are short but I seem to run into placement issues with long labels… Any ideas on better ways to handle this?

  • Frame produced by Mathematica seems to overlap with the labels…

Any suggestions gladly appreciated!

$\endgroup$
2
  • $\begingroup$ You are aware of the ParetoPlot function? $\endgroup$ Commented Apr 1, 2013 at 16:58
  • $\begingroup$ Yes… same issues apply to Pareto as well.. I thought asking a question about BarCharts may garner more responses…. $\endgroup$
    – Pam
    Commented Apr 1, 2013 at 17:02

1 Answer 1

4
$\begingroup$

Here's one approach.

mydata = N[Round[{0.9436, 2.20725333345, 2.1402, 1.8729, 4.9436, 0.4819}*100]/100];

This will make the data appear with only 2 decimal places of accuracy. If you are willing to rearrange things a bit, it's pretty easy to remove the overlap...

BarChart[Labeled[#2, #1, Before] & @@@ a, BarSpacing -> 0, 
  LabelingFunction -> (Placed[#, After] &), BarOrigin -> Left, 
  PlotLabel -> "Some Title"]

This gives:

enter image description here

$\endgroup$
3
  • $\begingroup$ That works… why doesn’t N[mydata,2] work? $\endgroup$
    – Pam
    Commented Apr 1, 2013 at 17:26
  • 1
    $\begingroup$ Because the numbers you have are floating point... this would work if the argument were rational. For example, if you had expressed mydata as {47/50, 221/100, 107/50, 187/100, 247/50, 12/25}, then N[mydata,{4,3}] would do what you want. $\endgroup$
    – bill s
    Commented Apr 1, 2013 at 17:56
  • $\begingroup$ Makes sense… thanks $\endgroup$
    – Pam
    Commented Apr 1, 2013 at 18:18

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.