安装并加载所需R包
# install.packages("wordcloud2")
library(wordcloud2)
使用方法
wordcloud2(data, size = 1, minSize = 0, gridSize = 0,
fontFamily = 'Segoe UI', fontWeight = 'bold',
color = 'random-dark', backgroundColor = "white",
minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE,
rotateRatio = 0.4, shape = 'circle', ellipticity = 0.65,
widgetsize = NULL, figPath = NULL, hoverFunction = NULL)
常用参数
data 数据框格式,包括词语和词频
size 设置字体大小,默认为1
fontFamily 设置字体类型
fontWeight 设置字体宽度
color 设置文本颜色
backgroundColor 设置背景颜色
minRotation,maxRotation 设置字体旋转的角度
rotateRatio 设置旋转字体的比例
shape 设置词云形状,默认为圆形
figPath 设置所用图片的路径
查看示例数据
# 英文示例数据
head(demoFreq)
## word freq
## oil oil 85
## said said 73
## prices prices 48
## opec opec 42
## mln mln 31
## the the 26
# 中文示例数据
head(demoFreqC)
## V2 V1
## 1 数据 2304
## 3 统计 1413
## 4 用户 855
## 5 模型 846
## 7 分析 773
## 8 数据分析 750
示例数据格式
第一列为词语名,第二列为词频数
基础绘图
wordcloud2(demoFreq)
image.png
# 设置字体大小和宽度
wordcloud2(demoFreq, size = 2, fontWeight = "bold")
image.png
# shape参数设置词云展现图形
wordcloud2(demoFreq, size = 1,shape = 'pentagon')
image.png
wordcloud2(demoFreq, size = 1,shape = 'star')
image.png
# 设置字体颜色和背景色
wordcloud2(demoFreq, size = 2,
color = "random-light", backgroundColor = "grey")
image.png
# 设置字体旋转的角度和旋转比例
# 所有字体旋转90°
wordcloud2(demoFreq, size = 2, minRotation = -pi/2, maxRotation = -pi/2)
image.png
# 所有字体旋转30°
wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = -pi/6,
rotateRatio = 1)
image.png
# 一半字体旋转30°
wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = pi/6,
rotateRatio = 0.5)
image.png
# 展示中文词频
wordcloud2(demoFreqC, size = 2,
color = "random-light", backgroundColor = "grey")
image.png
wordcloud2(demoFreqC, size = 2, fontFamily = "黑体",
color = "white", backgroundColor = "skyblue",
minRotation = -pi/6, maxRotation = -pi/6,
rotateRatio = 1)
image.png
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] wordcloud2_0.2.1
##
## loaded via a namespace (and not attached):
## [1] htmlwidgets_1.3 compiler_3.5.1 backports_1.1.2 magrittr_1.5
## [5] rprojroot_1.3-2 tools_3.5.1 htmltools_0.3.6 yaml_2.2.0
## [9] Rcpp_0.12.18 stringi_1.2.4 rmarkdown_1.10 knitr_1.20
## [13] jsonlite_1.5 stringr_1.3.1 digest_0.6.16 evaluate_0.11
网友评论