Written and maintained by CASRAI Editorial Board
Published
On this page
To make a word cloud in R, install the tm and wordcloud packages from CRAN, clean your text into a corpus, count each term across the corpus with TermDocumentMatrix(), and pass the words and their frequencies to wordcloud(words, freq). The function draws the plot and returns nothing, so the picture is the whole output. The same recipe covers every phrasing people use for it: r wordcloud, wordcloud r, word cloud with r and word cloud using r all mean this workflow, and wordcloud2 is a second package that draws an interactive HTML version from the same word-and-frequency table.
What a word cloud in R is used for in research
A word cloud is a frequency display: each word is drawn at a size that reflects how often it occurs in the text you supplied. In research practice that makes it an exploratory aid for open-ended survey answers, interview transcripts, abstracts or grant text, a quick look at which vocabulary dominates before you do any coding or modelling. The CRAN description of wordcloud is “Functionality to create pretty word clouds, visualize differences and similarity between documents, and avoid over-plotting in scatter plots with text.” The package therefore also offers comparison clouds, commonality clouds and a textplot function, which this page leaves aside.
Two cautions belong in any methods note. First, size encodes raw frequency only: a large word is a frequent word, not an important, positive or representative one. Second, the picture depends on choices you make before plotting, namely which words are removed, the minimum frequency and the maximum number of words shown. Because those choices change what a reader sees, record them with your analysis. A cloud is a descriptive display, not a test; if you need to analyse text systematically, a coding approach such as content analysis or the methods in natural language processing gives you something you can defend, with the cloud as an illustration.
Install the packages
All three packages used here are on CRAN, the official R package repository, and install with install.packages(). The versions listed on the CRAN pages on 2026-11-15 were tm 0.7-20 (published 2026-09-30, GPL-3, depends on R 3.4.0 or later), wordcloud 2.6 (published 2018-08-24, LGPL-2.1, imports Rcpp) and wordcloud2 0.2.1 (published 2018-01-03, GPL-2, depends on R 3.1.0 or later). RColorBrewer supplies colour palettes and is used in the wordcloud examples.
install.packages(c('tm', 'wordcloud', 'RColorBrewer'))
# optional, for the interactive version
install.packages('wordcloud2')
If installation fails or you have never added a package before, see the guide to how to install R packages, and how to install R itself first if needed. Cite the packages you used; the guide on citing software, code and R packages shows how, and citation('wordcloud') prints a package’s recommended citation.
Step by step: build a word cloud with tm and wordcloud
The steps below start from a character vector in which each element is one document, for example one survey answer per element. Replace docs with your own text.
1. Load the packages and create a corpus
library(tm)
library(wordcloud)
library(RColorBrewer)
docs = c('First open-ended answer ...', 'Second open-ended answer ...')
corpus = VCorpus(VectorSource(docs))
In tm, VectorSource(x) interprets each element of a vector as a document, and a corpus is a collection of such documents; Corpus() is a convenience alias for SimpleCorpus or VCorpus depending on the arguments given. The documentation’s own example is VCorpus(VectorSource(docs)), which is why it is used here.
2. Clean the text with tm_map()
corpus = tm_map(corpus, content_transformer(tolower))
corpus = tm_map(corpus, removePunctuation)
corpus = tm_map(corpus, removeWords, stopwords('english'))
tm_map(x, FUN, ...) applies a transformation function to every document in a corpus and returns a corpus. content_transformer() wraps a plain character function such as tolower so it can get and set document content. removePunctuation removes punctuation marks; by default it works on the ASCII punctuation class, and its arguments preserve_intra_word_contractions and preserve_intra_word_dashes (both FALSE by default) keep contractions and hyphenated words intact if you set them to TRUE. removeWords takes a character vector of words to drop, and stopwords(kind = 'en') returns a stopword list. The lists documented include the SMART English list and Snowball lists for Danish, Dutch, English, Finnish, French, German, Hungarian, Italian, Norwegian, Portuguese, Russian, Spanish and Swedish. Language names are case sensitive.
3. Count the terms
tdm = TermDocumentMatrix(corpus)
freq = sort(rowSums(as.matrix(tdm)), decreasing = TRUE)
head(freq, 10)
TermDocumentMatrix(x, control = list()) builds a sparse matrix with one row per term and one column per document. Summing each row (rowSums, base R) gives the total count of each term, and sort(..., decreasing = TRUE) orders them. Printing the top terms with head() before you plot is worth doing: it shows whether stopwords, stray fragments or a dominant boilerplate word are driving the picture. Note that the termFreq options behind the matrix default to a minimum word length of 3 characters (wordLengths = c(3, Inf)), so one- and two-letter tokens never reach the cloud unless you change that in control.
The cleaning in step 2 can also be done inside the matrix call through its control list, for example TermDocumentMatrix(corpus, control = list(removePunctuation = TRUE, stopwords = TRUE)), which is the form used in the package’s own examples. The control options removeNumbers, stemming, tolower and bounds are also documented, with removeNumbers and stemming defaulting to FALSE and tolower to TRUE.
4. Draw the cloud
set.seed(42)
wordcloud(words = names(freq), freq = freq,
min.freq = 3, max.words = 100,
random.order = FALSE, rot.per = 0.1,
scale = c(4, 0.5),
colors = brewer.pal(8, 'Dark2'))
Setting a seed before plotting means any random choice the function makes, such as word order or colour assignment when you request them, can be reproduced the next time you run the script, which matters for a figure in a manuscript. This is the same discipline as any analysis step in a computationally reproducible workflow: keep the script, the package versions and the seed together.
Output and how to read it
wordcloud() draws onto the active graphics device and returns nothing. In RStudio the plot appears in the Plots pane; to save it, open a device such as png() before the call and close it with dev.off() afterwards. Larger words are more frequent, and with random.order = FALSE the most frequent words are plotted first. The default is random.order = TRUE. With the default colors of black, colour carries no information; with a palette and random.color = FALSE (the default), colour is assigned by frequency from least to most frequent, so it repeats what size already says. Setting random.color = TRUE colours at random, which is decorative only.
When you describe a cloud in a paper, say how many documents it comes from, what was removed (stopword list, punctuation, numbers, stemming), the min.freq and max.words used, and that size is raw frequency. Readers can then see that two clouds from different preprocessing are not directly comparable.
Arguments that change the picture
The table below lists the wordcloud() arguments that most affect the result, with the defaults stated in the package’s documentation page for wordcloud.
| Argument | Default | What it does |
|---|---|---|
words, freq |
none (required) | The words to plot and their frequencies. |
scale |
c(4, 0.5) |
Length-2 vector giving the largest and smallest word sizes. |
min.freq |
3 |
Words with frequency below this are not plotted. |
max.words |
no limit | Maximum number of words; the least frequent are dropped first. |
random.order |
TRUE |
Plots words in random order; FALSE plots from most to least frequent. |
random.color |
FALSE |
Chooses colours at random when TRUE; otherwise colour follows frequency. |
rot.per |
0.1 |
Proportion of words rotated 90 degrees. |
colors |
black | Colours from least to most frequent words. |
use.r.layout |
FALSE |
Uses C++ collision detection when FALSE; TRUE uses R. |
fixed.asp |
TRUE |
Keeps the aspect ratio fixed; it is only variable when rot.per = 0. |
Troubleshooting
Some words are missing from the cloud
This is the documented behaviour of two arguments: words below min.freq (default 3) are not plotted, and when max.words is set the least frequent words are dropped first. In a short text where most words occur once or twice, the default min.freq = 3 can remove nearly everything, so lower it (for example min.freq = 1) and check the result with head(freq). If the plot looks crowded, reduce the first value of scale or enlarge the graphics device.
Stopwords or meaningless fragments dominate
Print the sorted frequencies first. If words such as “the” appear, the stopword step did not run on the corpus you counted; if short fragments appear, they come from tokenisation, and the word-length bounds in the matrix control list can remove them. For domain filler (for example the study’s own name), pass a custom character vector to removeWords, for example c(stopwords('english'), 'study', 'participants'); removeWords accepts any character vector of words.
The layout is different on every run
Use set.seed() before wordcloud() and keep the same arguments. Use random.order = FALSE if you want a deterministic ranking of plotting order.
Words are not rotated, or the plot is not the shape you expected
Set rot.per to the proportion of words that should be turned 90 degrees (default 0.1). The plot keeps a fixed aspect ratio unless rot.per = 0, when the documentation says the aspect can vary.
Interactive and shaped clouds with wordcloud2
wordcloud2 is a separate package that wraps the wordcloud2.js JavaScript library as an htmlwidget. Its main function, wordcloud2(data, ...), requires a data frame whose columns are the word and its frequency, so convert your named vector first.
library(wordcloud2)
df = data.frame(word = names(freq), freq = as.numeric(freq))
wordcloud2(data = df, size = 1, shape = 'circle')
Documented arguments and defaults include size (1), minSize (0), gridSize (0), fontFamily (‘Segoe UI’), fontWeight (‘bold’), color (‘random-dark’, or ‘random-light’, or a vector of colours), backgroundColor (‘white’), minRotation and maxRotation (-pi/4 and pi/4), shuffle (TRUE), rotateRatio (0.4), shape (‘circle’) and ellipticity (0.65). The shape argument accepts ‘circle’, ‘cardioid’, ‘diamond’, ‘triangle-forward’, ‘triangle’, ‘pentagon’ and ‘star’. A mask image path can be given through figPath, and a hoverFunction can customise what appears on hover. Because the output is HTML, it suits a Shiny app (the package lists shiny under Enhances) or an R Markdown page; it is less suited to a print figure unless you capture a static image, which is why the package lists webshot in Suggests.
wordcloud or wordcloud2: which package fits
The two packages differ in output type, input and what each documents. The data table below compares them from their CRAN and documentation pages. In short, use wordcloud when you need a static plot in base graphics that you can save to a file for a manuscript, and wordcloud2 when you want an interactive page, a custom shape or hover text. Both take the same word-and-frequency information, so the counting steps above are shared.
Frequently asked questions
How do I make a word cloud in R?
Install tm and wordcloud from CRAN, build a corpus with VCorpus(VectorSource(docs)), clean it with tm_map(), count terms with TermDocumentMatrix(), and call wordcloud(names(freq), freq) on the sorted frequency vector.
What is the difference between r wordcloud and wordcloud2?
wordcloud (version 2.6, published 2018-08-24) draws a static plot and returns nothing; wordcloud2 (version 0.2.1, published 2018-01-03) is an htmlwidget that renders in a browser or Viewer, takes a data frame with word and freq columns, and offers shapes and mask images.
Can I make a word cloud in R without the tm package?
Yes in principle: wordcloud() only needs a character vector of words and a numeric vector of frequencies. Any method that produces that pair works, such as table() on a tokenised vector. The tm package is simply a documented route to clean and count text, described on CRAN as a framework for text mining applications within R.
Why do some of my words not appear?
Because words below min.freq (default 3) are not plotted, and a set max.words drops the least frequent words first. Lower min.freq or raise max.words.
How do I keep the layout the same between runs?
Call set.seed() before wordcloud() and keep the arguments unchanged, and report the seed and package versions with your analysis.
Is a word cloud a valid research result?
It is a descriptive display of word frequency. It does not show sentiment, meaning or statistical difference, so use it to illustrate a point and support it with a systematic analysis.
Sources
- CRAN, wordcloud package page (version 2.6): https://cran.r-project.org/web/packages/wordcloud/index.html, read 2026-11-15.
- rdrr.io, wordcloud function documentation: https://rdrr.io/cran/wordcloud/man/wordcloud.html, read 2026-11-15.
- CRAN, wordcloud2 package page (version 0.2.1): https://cran.r-project.org/web/packages/wordcloud2/index.html, read 2026-11-15.
- rdrr.io, wordcloud2 function documentation: https://rdrr.io/cran/wordcloud2/man/wordcloud2.html, read 2026-11-15.
- CRAN, tm package page (version 0.7-20): https://cran.r-project.org/web/packages/tm/index.html, read 2026-11-15.
- tm documentation for tm_map, TermDocumentMatrix, termFreq, stopwords, removeWords, removePunctuation, Corpus and VectorSource: https://rdrr.io/cran/tm/man/tm_map.html and sibling pages under the same path, read 2026-11-15.
Data: wordcloud and wordcloud2 R packages compared from CRAN and documentation
Side-by-side comparison of the wordcloud and wordcloud2 R packages: version, CRAN publication date, licence, output type, required data input, and distinctive documented features. Compiled from the packages' CRAN pages and their rdrr.io function documentation, read on 2026-11-15.
Period covered: 2018/2026.
| Attribute | wordcloud | wordcloud2 |
|---|---|---|
| Version on CRAN | 2.6 | 0.2.1 |
| CRAN publication date | 2018-08-24 | 2018-01-03 |
| Licence | LGPL-2.1 | GPL-2 |
| Output | Static plot drawn on the active graphics device; returns nothing | htmlwidget using wordcloud2.js |
| Required input | words and freq arguments (word vector and frequency vector) | data frame with word and freq columns |
| Documented defaults for colour | colors = black, least to most frequent | color = 'random-dark' |
| Documented shape control | rot.per sets the proportion of words rotated 90 degrees (default 0.1) | shape argument: circle, cardioid, diamond, triangle-forward, triangle, pentagon, star; figPath mask image |
| Declared dependencies | Imports Rcpp (>= 0.9.4) | Depends R >= 3.1.0; Imports htmlwidgets, base64enc, grDevices, graphics; Suggests knitr, rmarkdown, webshot; Enhances shiny |
Sources
- CRAN wordcloud package page (accessed 2026-11-15)
- wordcloud function documentation (rdrr.io) (accessed 2026-11-15)
- CRAN wordcloud2 package page (accessed 2026-11-15)
- wordcloud2 function documentation (rdrr.io) (accessed 2026-11-15)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







