Message Boards Message Boards

GROUPS:

[✓] Create a letter frequency BarChart?

Posted 12 days ago
160 Views
|
2 Replies
|
3 Total Likes
|

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
Answer
2 Replies
Posted 12 days 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 11 days ago

Perfect, thanks Rohit!

POSTED BY: Jess M
Answer
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