首先安装和加载wordcloud2包
> install.packages("wordcloud2")
> library(wordcloud2)
R wordcloud包中wordcloud2()函数介绍:
> wordcloud2(data, size = 1, minSize = 0, gridSize = 0, fontFamily = NULL, fontWeight = 'normal', color = 'random-dark', backgroundColor = "white", minRotation = -pi/4, maxRotation = pi/4, rotateRatio = 0.4, shape = 'circle', ellipticity = 0.65, widgetsize = NULL)
常用参数介绍:
(1)data:词云生成数据,包含具体词语以及频率;
(2)size:字体大小,默认为1,一般来说该值越小,生成的形状轮廓越明显;
(3)fontFamily:字体,如‘微软雅黑’;
(4)fontWeight:字体粗细,包含‘normal’,‘bold’以及‘600’;
(5)color:字体颜色,可以选择‘random-dark’以及‘random-light’,其实就是颜色色系;
(6)backgroundColor:背景颜色,支持R语言中的常用颜色,如‘gray’,‘blcak’,但是还支持不了更加具体的颜色选择,如‘gray20’;
(7)minRontatin与maxRontatin:字体旋转角度范围的最小值以及最大值,选定后,字体会在该范围内随机旋转;
(8)rotationRation:字体旋转比例,如设定为1,则全部词语都会发生旋转;
(9)shape:词云形状选择,默认是‘circle’,即圆形。还可以选择‘cardioid’(苹果形或心形),‘star’(星形),‘diamond’(钻石),‘triangle-forward’(三角形),‘triangle’(三角形),‘pentagon’(五边形);
1、画星星图
> wordcloud2(demoFreq, size = 1,shape = 'star')
2、中文词云
> wordcloud2(demoFreqC, size = 2, fontFamily = "微软雅黑", color = "random-light", backgroundColor = "grey")
3、旋转固定角度
> wordcloud2(demoFreq, size = 2, minRotation = -pi/2, maxRotation = -pi/2)
> wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = -pi/6,rotateRatio = 1)
4、根据设定字符画词云图
> letterCloud(demoFreqC,word = "R",size = 2)
> letterCloud(demoFreqC,word="贤",size = 2)
以上例子画出的词云图形状都是系统自带的图形,或特定的常用字符。下面讲如何画自定义图形词云图。
(1)读取形状图片
> jordan = system.file("examples/jordan.png",package = "wordcloud2") #读取形状图片,注意要提前把图片放在wordcloud2包下面的examples文件夹中
(2)绘制词云图
> wordcloud2(demoFreq, figPath = jordan, size = 1,color = "black") #绘制星云图,其中demoFreg即为所用的数据,figPath = jordan为所用的自定义图片