The word cloud in R – A complete guide

Filed Under: R Programming
THE WORD CLOUD PLOTS IN R

A word cloud in R is often called as the text clouds. It is the visual appealing representation of the data which highlights the most frequently used or repeated words.

Hello folks, today, we are going to discuss one of the visualization graphs i.e. word clouds.

Word clouds are widely used in data analysis to analyze and understand the behavior or the sentiment of the data.

In this article, we will be looking at various types of word clouds that we can plot in R.


Pros and Cons of Word Clouds in R

Pros of Word cloudsCons of Word clouds
Word clouds offer the supreme visually appealing text representation. They are quite simple. The size of the words will represent their volume in the data.Yes, word clouds are visually appealing and meaningful. But The word clouds will fail in the process of major decision making.
Word clouds are highly informative and they are one of the best tool for communicating your findings. Through the highlighted text, it facilitates the text analysis to understand the behaviour and sentiment of the data.You cannot use the word clouds for the quantitative or numerical data analysis as this process only includes the categorical data.
They are meaningful. You can easily extract the insights to draw the discussions over the problem.

The syntax of wordcoulds in R

Word clouds: Word clouds are the visually appealing representation of the text data in which the size of the words will represent their volume in the data.

wordcloud2(data,size,color,backgroundcolor,shape,minrotation,maxrotation)

Where:

  • data = The input data file
  • size = The size of the words which should be displayed in the plot.
  • color = The colour of the words in the plot.
  • backgroundcolor = To set the background color of the plot.
  • shape = The shape of the text data representation.
  • min/max rotation = The angle or the rotation of the words.

1. A simple wordcloud in R

Well, I hope you are excited to plot your first “word cloud” in R. In this section, we are going to install the word cloud package and import the required libraries to plot the simple word cloud in R.

#install package
installed.packages(wordcloud2)

#import library
library(wordcloud2)

#plots the simple word cloud in R
wordcloud2(data=demoFreq,size = 1.5)
Word Cloud 1
A simple word cloud in R

2. Adding custom colors to the text in word clouds

In this section, we are going to add the color to our text in the word clouds.

You can add custom colors as well as random dark and light colors for the text in the plot.

Let’s see how it works.

#import the library
library(wordcloud2)

#adds custom color for the text i.e. Red and Black
wordcloud2(data=demoFreq,size = 1.5,color = rep_len(c('Black','Red'),nrow(demoFreq)))
Word Cloud 2
Word cloud with custom colour

3. Adding a background color to a wordcloud

Well, you have plotted the custom colored word cloud in the above section. Let’s try to change the background color of the plot.

Let’s see how it works!

#imports the library
library(wordcloud2)

#changes the background colour of the plot with black 
wordcloud2(data=demoFreq,size = 1.5,color = 'random-light',backgroundColor = 'Black')
Word Cloud 4
Word cloud with Black background

4. Plotting a word cloud with custom shape

Yes, you can plot the word clouds in custom shapes such as star shape.

In this section, we are going to plot the word cloud which will be in the shape of star.

Let’s see how it works.

#imports the library
library(wordcloud2)

#plots the word cloud in star shape
wordcloud2(data=demoFreq,size = 0.5,shape = 'star')

star shape
word cloud with custom shape

5. Rotating the text using min and max ratios

In the word cloud you can rotate the text in the plots. The wordcloud2 function offers features like min and max rotation along with rotation ratio to facilitate the angles.

Let’s see how it works.

#inports the library 
library(wordcloud2)

#rotates the text
wordcloud2(demoFreq, size = 1, minRotation = -0.52, maxRotation = -0.52, rotateRatio = 2)

rotating words
Rotated text in word cloud plot

Wrapping Up

The word cloud in R is an excellent way of presenting the text in a data. These plots are visually appealing as well as informative.

You can make use of various parameters to plot a word cloud in R as shown in this article.

That’s all for now. Happy plotting!!!

More read: R documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages