R中的词云
时间:2020-02-23 14:43:58 来源:igfitidea点击:
R中的词云通常称为文本云。
数据的视觉吸引力在于突出显示了最常用或者重复的单词。
大家好,今天,我们将讨论一个可视化图形,即词云。
词云广泛用于数据分析,以分析和了解数据的行为或者情感。
在本文中,我们将研究可以在R中绘制的各种类型的词云。
R中词云的优缺点
Pros of Word clouds | Cons 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. |
R中wordcoulds的语法
词云:词云是文本数据的视觉吸引力表示,其中词的大小将表示其在数据中的容量。
wordcloud2(data,size,color,backgroundcolor,shape,minrotation,maxrotation)
其中:
数据=输入数据文件
size =应在图中显示的单词的大小。
color =绘图中单词的颜色。
backgroundcolor =设置绘图的背景色。
shape =文本数据表示形式的形状。
最小/最大旋转=单词的角度或者旋转。
1. R中的简单wordcloud
好吧,希望您很高兴在R中绘制第一个"词云"。
在本节中,我们将安装词云包并导入所需的库以在R中绘制简单的词云。
#install package installed.packages(wordcloud2) #import library library(wordcloud2) #plots the simple word cloud in R wordcloud2(data=demoFreq,size = 1.5)
R中的简单词云
2.将自定义颜色添加到词云中的文本
在本节中,我们将为云中的文本添加颜色。
您可以为绘图中的文本添加自定义颜色以及随机的深色和浅色。
让我们看看它是如何工作的。
#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)))
带有自定义颜色的文字云
3.为wordcloud添加背景色
好了,您已经在上一节中绘制了自定义彩色字云。
让我们尝试更改绘图的背景颜色。
让我们看看它是如何工作的!
#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')
4.绘制具有自定义形状的词云
是的,您可以以自定义形状(例如星形)绘制单词云。
在本节中,我们将绘制星形的单词云。
让我们看看它是如何工作的。
#imports the library library(wordcloud2) #plots the word cloud in star shape wordcloud2(data=demoFreq,size = 0.5,shape = 'star')
5.使用最小和最大比例旋转文本
在词云中,您可以旋转绘图中的文本。
wordcloud2函数提供最小和最大旋转以及旋转比等功能,以简化角度。
让我们看看它是如何工作的。
#inports the library library(wordcloud2) #rotates the text wordcloud2(demoFreq, size = 1, minRotation = -0.52, maxRotation = -0.52, rotateRatio = 2)