Simple Wordcloud    wordclouds

A wordcloud can be one of the best tools that allows us to visualize most of the words and terms contained in tweets. Although its main use is for exploratory purposes, they have the advantage to be understandable by most users, and to be visually attractive to the human eyes (if done adequately).

How to create a wordcloud?
Wordclouds are relatively simple to make. Here's the main recipe steps
Download some tweets (via twitteR or XML)
Extract the text from the tweets
Do some text manipulation (for cleaning & formatting)
Create a lexical Corpus and a TermDocumentMatrix (via tm)
Obtain words and their frequencies
Plot the wordcloud

Example 1: tweets via twitteR
Step 1: Load all the required packages

Step 2: Let's get some tweets in english containing the words "machine learning"
mach_tweets = searchTwitter("machine learning", n=500, lang="en")

Step 3: Extract the text from the tweets in a vector
mach_text = sapply(mach_tweets, function(x) x$getText())

Step 4: Construct the lexical Corpus and the Term Document Matrix
We use the function Corpus to create the corpus, and the function VectorSource to indicate that the text is in the character vector mach_text. In order to create the term-document matrix we apply different transformation such as removing numbers, punctuation symbols, lower case, etc.
# create a corpus
mach_corpus = Corpus(VectorSource(mach_text))

# create document term matrix applying some transformations
tdm = TermDocumentMatrix(mach_corpus,
   control = list(removePunctuation = TRUE,
   stopwords = c("machine", "learning", stopwords("english")),
   removeNumbers = TRUE, tolower = TRUE))

Step 5: Obtain words and their frequencies
# define tdm as matrix
m = as.matrix(tdm)
# get word counts in decreasing order
word_freqs = sort(rowSums(m), decreasing=TRUE) 
# create a data frame with words and their frequencies
dm = data.frame(word=names(word_freqs), freq=word_freqs)

Step 6: Let's plot the wordcloud
# plot wordcloud
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))

# save the image in png format
png("MachineLearningCloud.png", width=12, height=8, units="in", res=300)
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))