5
$\begingroup$

I have the histogram:

dataPnJ = StringLength[StringSplit[ExampleData[{"Text", "PrideAndPrejudice"}]]];
Histogram[dataPnJ]

I am trying to make the histogram show what percentage of the total pool is the string length representing. So like if 30% of the text is 3 letter words the bin will be labeled 3 and will go up to 30%.

$\endgroup$
5
$\begingroup$
Histogram[dataPnJ, {1}, "Probability", 
 Ticks -> {Automatic, {#, PercentForm @ #} & /@ Range[0, .5, .1]}, 
 LabelingFunction -> (Placed[ PercentForm @ #, Tooltip] &)]

enter image description here

You can also use HistogramList to get a list of bar heights and use it with BarChart:

heights = HistogramList[dataPnJ, {1}, "Probability"][[2]];

BarChart[Labeled[#, Rotate[PercentForm[#, {5, 3}], 90 Degree], Above] & /@ heights, 
 BarSpacing -> 0, 
 ChartLabels -> Range[Length @ heights], 
 Ticks -> {Automatic, {#, PercentForm @ #} & /@ Range[0, .5, .1]}, 
 ImageSize -> Large]

enter image description here

$\endgroup$
2
$\begingroup$

Riffing off of @kglr's excellent answer, we can go a little further and fulfill all your requirements regarding labeling of the bars within Histogram, without having to build our own BarChart:

dataPnJ = StringLength[StringSplit[ExampleData[{"Text", "PrideAndPrejudice"}]]];

Histogram[
 dataPnJ, {1}, "Probability",
 Ticks -> {Range[20], {#, PercentForm@#} & /@ Range[0, .5, .1]}, 
 LabelingFunction -> (Placed[
     Rotate[PercentForm@Round[#, 0.001], 90 Degree], Above
   ] &)
]

modified histogram using ticks as bar labels and percentages as bar readouts

$\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.