(This article was first published on Bommarito Consulting » r, and kindly contributed to R-bloggers)
Here’s one purely for fun – a wordcloud built from the Supreme Court’s opinion on Arizona et al. v United States. Word clouds, though certainly not the most scientific of visualization techniques, are often engaging and “fun” ways to lead into discussion on NLP or topic modeling.
The process to generate this image is entirely automated with Tika and R. We convert the PDF to text, strip, normalize, and stopword the text, then convert to histogram and plot as a wordcloud. Here’s the snippet below:
$ java -jar /opt/tika/tika.jar --text arizona_et_al_v_united_states.pdf > arizona_et_al_v_united_states.txt
$ R
> library(tm)
> library(wordcloud)
> library(RColorBrewer)
> corpus <- Corpus(DirSource(pattern="*.txt"))
> corpus <- tm_map(tm_map(tm_map(corpus, removePunctuation), tolower), function(x) removeWords(x, stopwords("english")))
> tdMatrix <- as.matrix(TermDocumentMatrix(corpus))
> tdMatrix <- sort(rowSums(tdMatrix), decreasing=T)
> freqDF <- data.frame(words=names(tdMatrix), freq=tdMatrix)
> colorPalette <- brewer.pal(8, "RdBu")
> png(file="arizona_v_usa_wordcloud.png", bg="black")
> wordcloud(freqDF$words, freqDF$freq, scale=c(8, 0.25), min.freq=10, random.order=F, rot.per=0.2, colors=colorPalette)
> dev.off()
To leave a comment for the author, please follow the link and comment on his blog: Bommarito Consulting » r.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...


Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).