The Wordcloud2 library
This article is originally published at https://www.r-graph-gallery.com
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.
This 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.
# library library(wordcloud2) # have a look to the example dataset head(demoFreq)
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.
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")
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")
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)
A nice chinese version
wordcloud2(demoFreqC, size = 2, fontFamily = "微软雅黑", color = "random-light", backgroundColor = "grey")
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.
Not what you are looking for ? Make a new search !
Please visit source website for post related comments.