The Wordcloud2 library

December 9, 2016
By

(This article was first published on Blog The R graph Gallery, and kindly contributed to R-bloggers)

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

# library
library(wordcloud2) 

# have a look to the example dataset
head(demoFreq)


196_wordcloud_ex1

 

 

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.

wordcloud2(demoFreq, size=1.6)


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

# 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("green","blue"), nrow(demoFreq) ) )

# Change the background color
wordcloud2(demoFreq, size=1.6, color='random-light', backgroundColor="black")


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

# Change the shape:
wordcloud2(demoFreq, size = 0.7, shape = 'star')

# Change the shape using your image
wordcloud2(demoFreq, figPath = "peace.png", size = 1.5, color = "skyblue", backgroundColor="black")


196_wordcloud_ex7

 

 

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

ww=wordcloud2(demoFreq, size = 2.3, minRotation = -pi/6, maxRotation = -pi/6, rotateRatio = 1)


196_wordcloud_ex8

 

 

A nice chinese version

wordcloud2(demoFreqC, size = 2, fontFamily = "????", color = "random-light", backgroundColor = "grey")


———–196_wordcloud_ex9 ——–196_wordcloud_ex10

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

letterCloud( demoFreq, word = "R", color='random-light' , backgroundColor="black")
letterCloud( demoFreq, word = "PEACE", color="white", backgroundColor="pink")


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:

#install webshot
library(webshot)
webshot::install_phantomjs()

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

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

# and in pdf
webshot("tmp.html","fig_1.pdf", delay =5, vwidth = 480, vheight=480)


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 !

3D abline add aggregate animation arrows at axis barplot basics beside bg border boxplot boxwex breaks bty cex cex.axis circular plot cmdscale col color Colors cor cut d3.js dataArt dendrogram density dist factor font ggplot ggplot2 hclust hist Histograms hiveplot horiz hsv ifelse Igraph image ImageMagick Important ! inset jitter labels las lattice layout leafletR legend legend.text levelplot levels library lines lm lty lwd main map Maps mar mfrow mfrox mgp Miscellaneous model mtext mtxt names nlevels oma order package par paste PCA pch pie plot plotly points polygon predict pt.cex pty R radarchart rect reorder rgb rgl Ring scatterplot segments stacked-area table tag_ tag_for_slider testnumber1 text tick Time series title toto tutu type VennDiagram violin-plot width wordcloud xaxt xlab xlim xyplot yaxt ylab ylim zetag

You can search a media by theme with the tag cloud above.

To leave a comment for the author, please follow the link and comment on their blog: Blog The R graph Gallery.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)