A word cloud (or tag cloud) is a visual representation of text data. Tags are usually single words, and the importance of each tag is shown with font size or color.

In R, two libraries will allow you to create wordclouds: Wordcloud and Wordcloud2. Wordcloud gives a simple wordcloud output whereas wordcloud2 helps in getting wordcloud in different shape, words and using images.

For creating a wordcloud we need words and their frequencies. So the data that is being used are the 7 days tweets on Delhi Air Pollution. The steps are as below :

1. Set up twitter oauth :

require(twitteR)
require(RCurl)

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

2. Fetch twitter data on “DelhiAirEmergency” :

delhipol_tweets<-searchTwitter("DelhiAirEmergency", since="2019-11-15",until="2019-11-21", n=500, lang="en")

Note : Here you have to mention the past 7 days as one can fetch only past 7 days tweets.

3. Preparing data :

library(tm)
delhipol_text<-sapply(delhipol_tweets, function(x) x$getText())

delhipol_corpus<-Corpus(VectorSource(delhipol_text))

delhipol_clean<-tm_map(delhipol_corpus, removePunctuation)

delhipol_clean<-tm_map(delhipol_clean, content_transformer(tolower))

delhipol_clean<-tm_map(delhipol_clean, function(x) iconv(x,"UTF-8","UTF-8", sub = ' '))

delhipol_clean<-tm_map(delhipol_clean, removeWords, stopwords("english"))

delhipol_clean<-tm_map(delhipol_clean, removeNumbers)

delhipol_clean<-tm_map(delhipol_clean, stripWhitespace)

#Removing Similar words or hashtags
delhipol_clean<-tm_map(delhipol_clean, removeWords, c("delhi","delhiairemergency","pollution","airpollution","delhiairpollution","delhipollution","indian","ncr","delhincr","india"))

tdm <- TermDocumentMatrix(delhipol_clean)
findFreqTerms(tdm)

findFreqTerms(tdm,10)

findAssocs(tdm, 'sim', 0.30)

m <- as.matrix(tdm) 
v <- sort(rowSums(m), decreasing=TRUE)
myNames <- names(v)
d <- data.frame(word=myNames, freq=v)

4. Generating wordcloud :

library(devtools)
devtools::install_github("lchiffon/wordcloud2")

Note : Install wordcloud2 package from github and not from cran

When the above code is executed, below permission is asked in console :

Enter : 3 Execute the following codes.

5. Creating wordcloud :

library(wordcloud2)

#Star shape :
wordcloud2(d,shape = 'star')

Available shapes are: circle, cardioid, diamond, triangle-forward, triangle, pentagon, star

Wordcloud in letter/s :

letterCloud(d,word = "AIR")

Wordcloud in form of a image : the image used is,

wordcloud2(d, figPath = "pollution.jpg")

Note : On executing wordcloud2() or lettercloud() function, if no image is displayed just refresh the viewer few times, you’ll get the image.

Also you can copy-paste the wordcloud by clicking on Export -> Copy to Clipboard.

Note : If the word cloud is not appearing after executing code or while copy just click refresh button or cpoy to clipboard button respectively a few times.