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 abstrally attractive to the human eyes (if done adequately ). How to Create a wordcloud? Wordclouds are relatively simple to make. here's the main recipe stepsdownload some tweets (via Twitter or XML) extract the text from the tweetsdo some text manipulation (for cleaning & formatting) create a lexical corpus and a termdocumentmatrix (via TM) Obtain Words and Their frequenciesplot the wordcloud Example 1: tweets via TwitterStep 1:Load all the required packageslibrary(twitteR)library(tm)library(wordcloud)library(RColorBrewer) 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 vectormach_text = sapply(mach_tweets, function(x) x$getText()) Step 4:Construct the lexical corpus and the term document matrixwe 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 corpusmach_corpus = Corpus(VectorSource(mach_text)) # create document term matrix applying some transformationstdm = 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 matrixm = as.matrix(tdm)# get word counts in decreasing orderword_freqs = sort(rowSums(m), decreasing=TRUE) # create a data frame with words and their frequenciesdm = data.frame(word=names(word_freqs), freq=word_freqs) Step 6:Let's plot the wordcloud # plot wordcloudwordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2")) # save the image in png formatpng("MachineLearningCloud.png", width=12, height=8, units="in", res=300)wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))dev.off() |