The Wordcloud2 library

A word cloud (or tag cloud) is a visual representation of text data. Tags are usually single words, and the importance of each tag is shown with font size or color. 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: Wordcloud and Wordcloud2. In this post I will introduce the main features of the awesome Wordcloud2 library developed by Chiffon Lang.

196_wordcloud_ex6This little tutorial is largely inspired from the well-done vignette of the package. Most of the credit goes to Chiffon Lang, 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.




As you can see, wordcloud2 takes a dataframe with 2 columns as input . The first column gives the words that will be displayed in the wordcloud, and the second column gives their relative prominences.

We start with the default wordcloud, without changing any argument. Simply adjust the size of the wordcloud using the size argument.

———————-196_wordcloud_ex2 196_wordcloud_ex3 196_wordcloud_ex4

Then, it is possible to change words and background colors with the color and backgroundColor arguments.

—————196_wordcloud_ex5 196_wordcloud_ex6

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’).

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.




Control the rotation of words with 3 arguments: minRotation, maxRotation and rotateRatio.




A nice chinese version

———–196_wordcloud_ex9 ——–196_wordcloud_ex10

The lettercloud function allows to use a letter or a word as a shape for the wordcloud.

Save wordcloud2 as PDF

Last but not least, let’s check how to save our wordcloud as a static .pdf image. Wordcloud2 is made from a html widget. It means your wordcloud will be output in a html format by default.

You can export it as a pdf image using rstudio, or using the webshot library as follow:

I hope these few lines of code will allow you to compute your wordclouds efficiently. You can find more wordcloud examples on the dedicated section of the R graph gallery. 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 Chiffon Lang for creating such a useful library.

Happy ploting!

Not what you are looking for ? Make a new search !

Leave a Reply

Your email address will not be published.