Message Boards Message Boards

0
|
4375 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

[?] Create a letter frequency BarChart?

Posted 5 years ago

Hi all,

I'm looking for a way to produce a bar chart that will display the frequencies of letters in a given string which would include empty bars for any letter which isn't in the string.

BarChart[LetterCounts["GIVEN STRING GOES HERE"], 
 ChartLabels -> Automatic]

Almost does what I want, but I would like to have the entire English upper case alphabet, in alphabetical order, as the horizontal labels, and have empty bars where any letter is not contained in the given string.

Also, any way to turn this into a relative frequency bar chart? I.e. have the vertical measurements as count(letter)/count(total) instead of count(letter)

Thanks for any help!

POSTED BY: Jess M
2 Replies
Posted 5 years ago

Jess,

One way to do this.

zeroCounts =  Alphabet[] // ToUpperCase // AssociationThread[#, ConstantArray[0, 26]] &;
BarChart[zeroCounts~Join~LetterCounts["GIVEN STRING GOES HERE"], ChartLabels -> Automatic]

enter image description here

To get the relative counts, just scale the values in the LetterCounts association by the total of the values.

counts = LetterCounts["GIVEN STRING GOES HERE"];
scaledCounts = counts / (counts // Values // Total)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Perfect, thanks Rohit!

POSTED BY: Jess M
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract