SlideShare a Scribd company logo
1 of 6
How to create a Word Cloud in R

Mona Swarnakar
Date: 9th January 2014
Email id: s.mona8@gmail.com
Blog: http://monaprofile.blogspot.in/
I have seen Word-cloud in so many places such as magazines, websites, blogs, etc. however
never thought of making it by myself. I would like to thank R that taught me such a wonderful
technique which everybody would like to learn. I don’t know if there are any other free options
are available to create word-cloud. Let me give a quick explanation about R first, R is a free
source packages and very useful for statistical analysis. We can use R for various purposes, from
data mining to data visualization. Word-cloud is a tool where you can highlight the words which
have been used the most in quick visualization. If you know the correct procedure and usage,
then Word Cloud is simple in R Studio. Further, a package called “Word-cloud” is released in R,
which will help us to create word-cloud. You can follow my simple four steps mentioned below
to create word-cloud.
Those are new to R or Word Cloud, I would suggest first install R studio from the link
rstudio.com
Also, the following packages are required to create word cloud in R, so install these following
packages as well:
library(twitteR)
library(tm)
library(SnowballC)
library(wordcloud)

Note: You can see on the right side of the image, there is an option of the packages you want to
install
Step 1->
First we have to install the below package in R:
library (twitteR)
Once installation is done, we will load the Tweets data from D drive (that you have saved in
your drive) in the below mentioned codes:
> load("F:MonaMona RTweets.RData")

For the Tweets to convert into a data frame, we will write the below codes;
>df=do.call("rbind",lapply(tweets, as.data.frame))
>dim(df)

Step 2 ->
Now install the below package:
library(tm)
Corpus is collection of data texts. VectorSource is a very useful command based on which we
can create a corpus of character vectors.
>mydata=Corpus(VectorSource(df$text))

Transformations: Once we have corpus we can modify the document (for example stopwords
removal, stemming, etc.). Transformations are done via tm_map () function which applies to all
elements of corpus and all transformation can be done in single text documents.
To clean the data file various commands are used, which are listed below:
To Eliminating extra white spaces:

> mydata=tm_map(mydata, stripWhitespace)

To Convert to Lower Case:
>mydata=tm_map(mydata, tolower)

To remove punctuations:
>mydata=tm_map(mydata,removePunctuation)

To remove numbers:

>mydata=tm_map(mydata, removeNumbers
Stopwords: A further preprocessing technique is the removal of stopwords. Stopwords are
words that are so common in a language that their information value is almost zero, in other
words their entropy is very low. Therefore it is usual to remove them before further analysis.
At first we set up a tiny list of stopwords:
In this we are adding “R” and “online” to remove from wordlist.
>my_stopwords=c(stopwords('english'),c('R','online'))
>mydata=tm_map(mydata, removeWords, my_stopwords)

Stemming: Stemming is the process of removing suffixes from words to get the common
origin. For example, remove ing, ed from word to make it simple. Another example would be we would like to count the words stopped and stopping as being the same and derived from
stop.

Step 3 ->
Now install the below package:
library(SnowballC)
>mydata=tm_map(mydata, stemDocument)

Term-Document Matrix:

A common approach in text mining is to create a term-document

matrix from a corpus. In the tm package the classes Term Document Matrix (tdm)and Document
Term Matrix(dtm) (depending on whether you want terms as rows and documents as columns,
or vice versa) employ sparse matrices for corpora.
>tdm<-TermDocumentMatrix(mydata)

Frequent Terms: Now we can have a look at the popular words in the term-document matrix.
>wordfreq=findFreqTerms(tdm, lowfreq=70)
>termFrequency=rowSums(as.matrix(tdm1[wordfreq,]))

Now we can have a look at the popular words in the term-document matrix.
Step 4 ->

Word Cloud: After building a term-document matrix and frequency terms, we can show the
importance of words with a word cloud.
Now install the below package:
library(wordcloud)
library(RColorBrewer)
pal2 <- brewer.pal(8,"Dark2")

There are three options; you can apply any one for different wordcloud colour:

>wordcloud(words=names(wordFreq),freq=wordFreq,min.freq=5,max.words=50,random.order=
F,colors="red")
>wordcloud(words=names(wordFreq),freq=wordFreq,scale=c(5,.2),min.freq=3,max.words= 200,
random.order=F, rot.per=.15, colors=brewer.pal(8, "Dark2"))
>wordcloud(words=names(wordFreq),freq=wordFreq, scale=c(5,.2),min.freq=3, max.words=Inf,
random.order=F,rot.per=.15,random.color=TRUE,colors=rainbow(7))

To get multiple colour in word cloud we use (pal2 <- brewer.pal(8,"Dark2"), if you want only
one colour in word cloud you can simply write “red” or “blue” in colour option.

The above word cloud clearly shows that "data", "example" and "research" are the three most
important words, which validates that the in twitter these words have been used the most.
o
o
o
o
o
o
o
o
o
o

Words: the words
Freq: their frequencies
Scale: A vector of length 2 indicating the range of the size of the words.
min.freq: words with frequency below min.freq will not be plotted
max.words: Maximum number of words to be plotted. least frequent terms dropped
random.order: plot words in random order. If false, they will be plotted in decreasing
frequency
random.color: choose colors randomly from the colors. If false, the color is chosen
based on the frequency
rot.per: proportion words with 90 degree rotation
Colors color words from least to most frequent
Ordered.colors if true, then colors are assigned to words in order

Hope this helps.

Thanks for reading………….

More Related Content

Recently uploaded

Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfAnna Loughnan Colquhoun
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataSafe Software
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceMartin Humpolec
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?SANGHEE SHIN
 

Recently uploaded (20)

Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdf
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your Salesforce
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?
 

How to create a word cloud in R

  • 1. How to create a Word Cloud in R Mona Swarnakar Date: 9th January 2014 Email id: s.mona8@gmail.com Blog: http://monaprofile.blogspot.in/
  • 2. I have seen Word-cloud in so many places such as magazines, websites, blogs, etc. however never thought of making it by myself. I would like to thank R that taught me such a wonderful technique which everybody would like to learn. I don’t know if there are any other free options are available to create word-cloud. Let me give a quick explanation about R first, R is a free source packages and very useful for statistical analysis. We can use R for various purposes, from data mining to data visualization. Word-cloud is a tool where you can highlight the words which have been used the most in quick visualization. If you know the correct procedure and usage, then Word Cloud is simple in R Studio. Further, a package called “Word-cloud” is released in R, which will help us to create word-cloud. You can follow my simple four steps mentioned below to create word-cloud. Those are new to R or Word Cloud, I would suggest first install R studio from the link rstudio.com Also, the following packages are required to create word cloud in R, so install these following packages as well: library(twitteR) library(tm) library(SnowballC) library(wordcloud) Note: You can see on the right side of the image, there is an option of the packages you want to install
  • 3. Step 1-> First we have to install the below package in R: library (twitteR) Once installation is done, we will load the Tweets data from D drive (that you have saved in your drive) in the below mentioned codes: > load("F:MonaMona RTweets.RData") For the Tweets to convert into a data frame, we will write the below codes; >df=do.call("rbind",lapply(tweets, as.data.frame)) >dim(df) Step 2 -> Now install the below package: library(tm) Corpus is collection of data texts. VectorSource is a very useful command based on which we can create a corpus of character vectors. >mydata=Corpus(VectorSource(df$text)) Transformations: Once we have corpus we can modify the document (for example stopwords removal, stemming, etc.). Transformations are done via tm_map () function which applies to all elements of corpus and all transformation can be done in single text documents. To clean the data file various commands are used, which are listed below: To Eliminating extra white spaces: > mydata=tm_map(mydata, stripWhitespace) To Convert to Lower Case: >mydata=tm_map(mydata, tolower) To remove punctuations: >mydata=tm_map(mydata,removePunctuation) To remove numbers: >mydata=tm_map(mydata, removeNumbers
  • 4. Stopwords: A further preprocessing technique is the removal of stopwords. Stopwords are words that are so common in a language that their information value is almost zero, in other words their entropy is very low. Therefore it is usual to remove them before further analysis. At first we set up a tiny list of stopwords: In this we are adding “R” and “online” to remove from wordlist. >my_stopwords=c(stopwords('english'),c('R','online')) >mydata=tm_map(mydata, removeWords, my_stopwords) Stemming: Stemming is the process of removing suffixes from words to get the common origin. For example, remove ing, ed from word to make it simple. Another example would be we would like to count the words stopped and stopping as being the same and derived from stop. Step 3 -> Now install the below package: library(SnowballC) >mydata=tm_map(mydata, stemDocument) Term-Document Matrix: A common approach in text mining is to create a term-document matrix from a corpus. In the tm package the classes Term Document Matrix (tdm)and Document Term Matrix(dtm) (depending on whether you want terms as rows and documents as columns, or vice versa) employ sparse matrices for corpora. >tdm<-TermDocumentMatrix(mydata) Frequent Terms: Now we can have a look at the popular words in the term-document matrix. >wordfreq=findFreqTerms(tdm, lowfreq=70) >termFrequency=rowSums(as.matrix(tdm1[wordfreq,])) Now we can have a look at the popular words in the term-document matrix.
  • 5. Step 4 -> Word Cloud: After building a term-document matrix and frequency terms, we can show the importance of words with a word cloud. Now install the below package: library(wordcloud) library(RColorBrewer) pal2 <- brewer.pal(8,"Dark2") There are three options; you can apply any one for different wordcloud colour: >wordcloud(words=names(wordFreq),freq=wordFreq,min.freq=5,max.words=50,random.order= F,colors="red") >wordcloud(words=names(wordFreq),freq=wordFreq,scale=c(5,.2),min.freq=3,max.words= 200, random.order=F, rot.per=.15, colors=brewer.pal(8, "Dark2")) >wordcloud(words=names(wordFreq),freq=wordFreq, scale=c(5,.2),min.freq=3, max.words=Inf, random.order=F,rot.per=.15,random.color=TRUE,colors=rainbow(7)) To get multiple colour in word cloud we use (pal2 <- brewer.pal(8,"Dark2"), if you want only one colour in word cloud you can simply write “red” or “blue” in colour option. The above word cloud clearly shows that "data", "example" and "research" are the three most important words, which validates that the in twitter these words have been used the most.
  • 6. o o o o o o o o o o Words: the words Freq: their frequencies Scale: A vector of length 2 indicating the range of the size of the words. min.freq: words with frequency below min.freq will not be plotted max.words: Maximum number of words to be plotted. least frequent terms dropped random.order: plot words in random order. If false, they will be plotted in decreasing frequency random.color: choose colors randomly from the colors. If false, the color is chosen based on the frequency rot.per: proportion words with 90 degree rotation Colors color words from least to most frequent Ordered.colors if true, then colors are assigned to words in order Hope this helps. Thanks for reading………….