1
$\begingroup$

I want to make the following bar chart, but with relative abundances on top of the chart labels. I get the absolute numbers, but not the relative abundances:

data = {{43, 14}, {193, 158}, {37, 18}, {12, 3}, {5, 1}, {4, 0}};

BarChart[data
 , LabelingFunction -> (Placed[#, Above] &)
 ]

I get it to work if I have only one bar per category, but not with two bars per category, because I don't know how to make the division inside the LabelingFunction separate for each category:

data = {43, 193, 37, 12, 5, 4};

BarChart[data
 , LabelingFunction -> (Placed[N[#/Total@data], Above] &)
 ]
$\endgroup$

1 Answer 1

2
$\begingroup$
labeleddata = Transpose@Map[Labeled[#, Rotate[#2, Pi/2], Above] & @@ # &, 
                  Transpose /@ ({#, N@Normalize[#, Total]} & /@ Transpose@data), {2}]; 

BarChart[labeleddata]

enter image description here

or

labeleddata2 = Transpose[Labeled[#, Rotate[#2, Pi/2], Above] & @@@ # & /@ 
                          (Transpose /@ ({#, N@Normalize[#, Total]} & /@ Transpose@data))];

BarChart[labeleddata2]
(* same picture *)
$\endgroup$
1
  • $\begingroup$ Great! Thanks a lot. I am sort of glad there isn't a simple answer. $\endgroup$ Jan 29, 2015 at 12:24

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.