The Mueller Report Word Cloud: A brief tutorial in R
Word clouds are simple visual summaries of the mostly frequently used words in a text, presenting essentially the same information as a histogram but are somewhat less precise and vastly more eye-catching. Get a quick sense of the themes in the recently released Mueller Report and its 448 pages of legal content.
By Rick Klein, Université Grenoble Alpes
This is a quick and dirty tutorial for generating a word cloud from the Mueller report using R. Code is available on GitHub (https://github.com/raklein/mueller-wordcloud), and all credit goes to Alboukadel Kassambara’s word cloud tutorial (http://www.sthda.com/english/wiki/text-mining-and-word-cloud-fundamentals-in-r-5-simple-steps-you-should-know) because I simply implemented an abbreviated version and added a few lines of code.
Word clouds are simple visual summaries of the mostly frequently used words in a text, after removing common and uninformative words. They present essentially the same information as a histogram but are somewhat less precise and vastly more eye-catching. They give you a quick sense for the themes in a text, which is useful when you’re dealing with 448 pages of legal content.
First, load our required libraries. We’ll be using:
Now downloaded the report and place it in your working directory (e.g., https://cdn.cnn.com/cnn/2019/images/04/18/mueller-report-searchable.pdf)
Convert the pdf to text, and store it as a character vector. Mistakes will be made here, but it worked OK for this document.
Convert that text string to a corpus so the tm package can work with it
Here we implement several steps to "tidy up" the corpus and remove common words that wouldn’t be very informative in a word cloud for our purposes. These are straight from Kassambara’s tutorial:
Convert the text to lower case
Remove common English words
Specify any additional words you want removed
Revisit this line after doing the visualization and add any extras
Eliminate extra white spaces
There's a randomization component (mostly in terms of layout), so let's lock that randomization so we can reproduce the exact same figure if we want to
Before making the figure, we're going to specify a graphics device to output to
I'm using Cairo because it adds antialiasing for higher quality
Turn off the Cairo graphics device, which effectively saves the wordcloud as a .png
Done! You should have a beautiful, semi-informative figure. Note that this figure was originally made in about 20 minutes, and I’ve received lots of suggestions for ways to make it better. Experiment!
Bio: Rick Klein is a post-doctoral researcher at the Université Grenoble Alpes. Interested in the reproducibility of psychological science across contexts.
- Machine Learning Finds “Fake News” with 88% Accuracy
- Generating Text with RNNs in 4 Lines of Code
- Find Out What Celebrities Tweet About the Most