<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-wordcloud2-library/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-graph-gallery.com/2016/12/09/the-wordcloud2-library/"> Blog – The R graph Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A <strong><a target="_blank" rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/wordcloud/">word cloud</a> </strong>(or<strong> tag</strong><b> cloud</b>) is a visual representation of text data. Tags are usually single words, and the importance of each tag is shown with font <strong>size or color. </strong>This mode of representation is useful for quickly perceiving the most prominent terms in a list and determine their relative prominences. In R, two libraries will allow you to create wordclouds: <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/wordcloud/wordcloud.pdf">Wordcloud</a> and <a target="_blank" rel="nofollow" href="https://github.com/Lchiffon/wordcloud2">Wordcloud2</a>. In this post I will introduce the main features of the awesome Wordcloud2 library developed by <a target="_blank" rel="nofollow" href="https://github.com/Lchiffon">Chiffon Lang</a>.</p>
<p><span></span></p>
<p><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex6" class="wp-image-2416" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex6.png?resize=456%2C456" data-recalc-dims="1">This little tutorial is largely inspired from the well-done <a target="_blank" rel="nofollow" href="https://github.com/Lchiffon/wordcloud2">vignette</a> of the package. Most of the credit goes to <a target="_blank" rel="nofollow" href="https://github.com/Lchiffon">Chiffon Lang</a>, but this package clearly feats in the gallery. In order to make our first tagcloud, we need to load the library which contains an example dataset.</p>

<pre># library
library(wordcloud2) 

# have a look to the example dataset
head(demoFreq)</pre>

<hr>
<p><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex1" class="wp-image-2424" src="https://i1.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex1.png?resize=456%2C467" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p>As you can see, wordcloud2 takes a <strong>dataframe</strong> with <strong>2 columns</strong> as input . The first column gives the words that will be displayed in the wordcloud, and the second column gives their relative prominences.</p>
<p>We start with the default wordcloud, without changing any argument. Simply adjust the size of the wordcloud using the size argument.</p>
<pre>wordcloud2(demoFreq, size=1.6)</pre>

<hr>
<p><span>———————-</span><img sizes="(max-width: 293px) 100vw, 293px" alt="196_wordcloud_ex2" class="wp-image-2412" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex2-293x300.png?resize=293%2C300" data-recalc-dims="1"> <img sizes="(max-width: 293px) 100vw, 293px" alt="196_wordcloud_ex3" class="wp-image-2413" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex3-293x300.png?resize=293%2C300" data-recalc-dims="1"> <img sizes="(max-width: 293px) 100vw, 293px" alt="196_wordcloud_ex4" class="wp-image-2414" src="https://i1.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex4-293x300.png?resize=293%2C300" data-recalc-dims="1"></p>
<p>Then, it is possible to change words and background colors with the color and backgroundColor arguments.</p>

<pre># Gives a proposed palette
wordcloud2(demoFreq, size=1.6, color='random-dark')

# or a vector of colors. vector must be same length than input data
wordcloud2(demoFreq, size=1.6, color=rep_len( c(&quot;green&quot;,&quot;blue&quot;), nrow(demoFreq) ) )

# Change the background color
wordcloud2(demoFreq, size=1.6, color='random-light', backgroundColor=&quot;black&quot;)</pre>

<hr>
<p><span>—————</span><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex5" class="wp-image-2422" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex5.png?resize=456%2C467" data-recalc-dims="1"> <img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex6" class="wp-image-2416" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex6.png?resize=456%2C456" data-recalc-dims="1"></p>
<p>It is possible to change the shape of the wordcloud. Several shapes are available within the package: ‘circle’ (default), ‘cardioid’, ‘diamond’ (alias of square), ‘triangle-forward’, ‘triangle’, ‘pentagon’, and ‘star’).</p>
<p>It is also possible to use any image you have as a mask! Just insert the image in the current working directory and use it as in the code below.</p>
<pre># Change the shape:
wordcloud2(demoFreq, size = 0.7, shape = 'star')

# Change the shape using your image
wordcloud2(demoFreq, figPath = &quot;peace.png&quot;, size = 1.5, color = &quot;skyblue&quot;, backgroundColor=&quot;black&quot;)</pre>

<hr>
<p><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex7" class="wp-image-2417" src="https://i2.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex7.png?resize=456%2C467" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p>Control the rotation of words with 3 arguments: minRotation, maxRotation and rotateRatio.</p>
<pre>ww=wordcloud2(demoFreq, size = 2.3, minRotation = -pi/6, maxRotation = -pi/6, rotateRatio = 1)</pre>

<hr>
<p><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex8" class="wp-image-2418" src="https://i2.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex8.png?resize=456%2C467" data-recalc-dims="1"></p>
<p> </p>
<p> </p>
<p>A nice chinese version</p>
<pre>wordcloud2(demoFreqC, size = 2, fontFamily = &quot;微软雅黑&quot;, color = &quot;random-light&quot;, backgroundColor = &quot;grey&quot;)</pre>

<hr>
<p><span>———–</span><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex9" class="wp-image-2419" src="https://i2.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex9.png?resize=456%2C456" data-recalc-dims="1"> <span>——–</span><img sizes="(max-width: 480px) 100vw, 480px" alt="196_wordcloud_ex10" class="wp-image-2420" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2016/11/196_wordcloud_ex10.png?resize=456%2C456" data-recalc-dims="1"></p>
<p>The lettercloud function allows to use a letter or a word as a shape for the wordcloud.</p>

<pre>letterCloud( demoFreq, word = &quot;R&quot;, color='random-light' , backgroundColor=&quot;black&quot;)
letterCloud( demoFreq, word = &quot;PEACE&quot;, color=&quot;white&quot;, backgroundColor=&quot;pink&quot;)</pre>

<hr>
<h3><span><strong>Save wordcloud2 as PDF</strong></span></h3>
<p>Last but not least, let’s check how to save our wordcloud as a static .pdf image. Wordcloud2 is made from a <a target="_blank" rel="nofollow" href="http://www.htmlwidgets.org/">html widget</a>. It means your wordcloud will be output in a <strong>html format</strong> by default.</p>
<p>You can export it as a <strong>pdf</strong> image using rstudio, or using the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/webshot/vignettes/intro.html">webshot library</a> as follow:</p>
<pre>#install webshot
library(webshot)
webshot::install_phantomjs()

# Make the graph
my_graph=wordcloud2(demoFreq, size=1.5)

# save it in html
library(&quot;htmlwidgets&quot;)
saveWidget(my_graph,&quot;tmp.html&quot;,selfcontained = F)

# and in pdf
webshot(&quot;tmp.html&quot;,&quot;fig_1.pdf&quot;, delay =5, vwidth = 480, vheight=480)</pre>

<hr>
<p>I hope these few lines of code will allow you to compute your wordclouds efficiently. You can find more wordcloud examples on the <a target="_blank" rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/wordcloud/">dedicated section</a> of the<a target="_blank" rel="nofollow" href="http://www.r-graph-gallery.com/"> R graph gallery</a>. Of course, do not hesitate to share your charts with the gallery if you used specific features that were not described above. And once more, thank you to <a target="_blank" rel="nofollow" href="https://github.com/Lchiffon">Chiffon Lang</a> for creating such a useful library.</p>
<p>Happy ploting!</p>
<hr>
<p>Not what you are looking for ? Make a new search !<code><br>



<p></p>

<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-graph-gallery.com/2016/12/09/the-wordcloud2-library/"> Blog – The R graph Gallery</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/WN9gXZ7REYg"></a></div></code>