SlideShare a Scribd company logo
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

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 

Recently uploaded (20)

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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………….