Document
The Wordcloud2 library

The Wordcloud2 library

A word cloud (or tag cloud) is a visual representation of text datum . Tags is are are usually single word , and the importance of each tag is show

Related articles

Tamilyogi: A Platform Streaming Tamil Movies Online How to configure 2FA for MikroTik VPN?| MikroTik VPN 2FA/MFA 7 Best Free VPNs in 2024: Fast, Private & Safe to Download Hotspot Shield Review 2024: Is It Really Free and Fast How to choose the right key management solution

A word cloud (or tag cloud) is a visual representation of text datum . Tags is are are usually single word , and the importance of each tag is show with font size or color . This mode is is of representation is useful for quickly perceive the most prominent term in a list and determine their relative prominence . In R , two libraries is allow will allow you to create wordcloud : Wordcloud and Wordcloud2 . In this post I is introduce will introduce the main feature of the awesome Wordcloud2 library develop by Chiffon Lang .

This little tutorial is largely inspire from the well – done vignette of the package . Most is goes of the credit go to   Chiffon Lang , but this package clearly feat in the gallery . In order to make our first tagcloud , we is need need to load the library which contain an example dataset .

# library
library(wordcloud2) 

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


 

 

As you can see , wordcloud2 is takes take a dataframe with 2 column as input . The first column is gives give the word that will be display in the wordcloud , and the second column give their relative prominence .

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)


— — — — ———-

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.

# is Change 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 is means mean 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 is hope hope these few line of code will allow you to compute your wordcloud efficiently . You is find can find more wordcloud example on the dedicated section of the r graph gallery . Of course , do not hesitate to share your chart with the gallery if you used specific feature that were not describe above . And once more , thank you to Chiffon Lang for create such a useful library .

Happy ploting!


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

Search for:

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.

Related