DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Application Modernization and 6 R's
  • What Are the 7 Rs of Cloud Migration Strategy?
  • Google Cloud “GCP” native NixOS images build
  • How to Build Your Own AI SRE From Scratch

Trending

  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Going All In On the JVM 25 Virtual Threads
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Build a Word Cloud With R

How to Build a Word Cloud With R

When building word clouds with R, it's most important to set random.order to false so that the words with the highest frequency are plotted first.

By 
Jitendra Bafna
·
Mar. 20, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this article, we are going to see how to build a word cloud with R. Word cloud is a text mining technique that allows us to highlight the most frequently used keywords in paragraphs of text. This technique is sometimes referred to as text clouds or tag clouds, which is a visual representation of text data.

First, we need to load the CSV data and then load the required library for building the word cloud.

##Install Packages
install.packages("tm")  # for text mining
install.packages("SnowballC") # for text stemming
install.packages("wordcloud") # word-cloud generator 
install.packages("RColorBrewer") # color palettes

##Load Require Library

library(tm)
library(SnowballC)
library(RColorBrewer)
library(wordcloud)

##Read the Data

tweetsDS<-read.csv("Tweets.csv")
tweetsDS<-data.frame(tweetsDS)

Now, we need to create the corpus of data.

## Calculate Corpus
tweetsDS.Corpus<-Corpus(VectorSource(tweetsDS$content))

We need to clean the data. This includes removing stopwords, numbers, whitespace, etc. and converting the corpus into a plain text document.

##Data Cleaning and Wrangling

tweetsDS.Clean<-tm_map(tweetsDS.Corpus, PlainTextDocument
tweetsDS.Clean<-tm_map(tweetsDS.Corpus,tolower)
tweetsDS.Clean<-tm_map(tweetsDS.Clean,removeNumbers)
tweetsDS.Clean<-tm_map(tweetsDS.Clean,removeWords,stopwords("english"))
tweetsDS.Clean<-tm_map(tweetsDS.Clean,removePunctuation)
tweetsDS.Clean<-tm_map(tweetsDS.Clean,stripWhitespace)
tweetsDS.Clean<-tm_map(tweetsDS.Clean,stemDocument)

Now, we are going to plot the word cloud.

wordcloud(tweetsDS.Clean,max.words = 200,random.color = TRUE,random.order=FALSE)

Image title

For an attractive and colorful word cloud, use the below code.

wordcloud(words = tweetsDS.Clean, min.freq = 1,
          max.words=100, random.order=FALSE, rot.per=0.35, 
          colors=brewer.pal(8, "Dark2"))

Image title

Here are a couple of important definitions.

  • scale: Indicates the range of sizes of the words.
  • max.words: Plots the specified number of words and discard least frequent terms.]
  • min.freq: Discards all terms whose frequency is below the specified value.
  • random.order: By setting this to false, we make it so that the words with the highest frequency are plotted first. If we don’t set this, it will plot the words in a random order and the highest frequency words may not necessarily appear in the center.
  • rot.per: Determines the fraction of words that are plotted vertically.
  • colors: The default value is black. If you want to use different colors based on frequency, you can specify a vector of colors or use one of the pre-defined color palettes.

I hope you have enjoyed learning how to build word cloud with R.

R (programming language) Cloud Build (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • Application Modernization and 6 R's
  • What Are the 7 Rs of Cloud Migration Strategy?
  • Google Cloud “GCP” native NixOS images build
  • How to Build Your Own AI SRE From Scratch

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook