学习 R 语言词云wordcloud--笔记

来源:互联网 发布:各省人口普查数据 编辑:程序博客网 时间:2024/06/05 10:17

wordcloud函数–用于绘制词云图

用法:
wordcloud(words,freq,scale=c(4,.5),min.freq=3,max.words=Inf,random.order=TRUE, random.color=FALSE, rot.per=.1,
colors=”black”,ordered.colors=FALSE,use.r.layout=FALSE,fixed.asp=TRUE, …)

参数
1)words — 关键词列表
the words
2)freq—关键词对应的词频列表
their frequencies
3)scale—显示字体大小的范围,例如c(3,0.3),最大字体是3,最小字体是0.3
A vector of length 2 indicating the range of the size of the words
4)min.freq—最小词频,低于最小词频的词不会被显示
words with frequency below min.freq will not be plotted
5)max.words—显示的最大词数量。
Maximum number of words to be plotted. least frequent terms dropped
6)random.order—词在图上的排列顺序。T:词随机排列;F:词按频数从图中心位置往外降序排列,即频数大的词出现在中心位置。
plot words in random order. If false, they will be plotted in decreasing frequency
7)random.color—控制词的字体颜色。T:字体颜色随机分配;F:根据频数分配字体颜色。
choose colors randomly from the colors. If false, the color is chosen based on the frequency
8)rot.per—控制词摆放角度。T:旋转90度;F:水平摆放。
proportion words with 90 degree rotation
9)colors—字体颜色列表
color words from least to most frequent
10)ordered.colors—控制字体颜色使用顺序。T:按照指定的顺序给出每个关键词字体颜色,(似乎是要求颜色列表中每个颜色一一对应关键词列表);F:任意给出字体颜色。
if true, then colors are assigned to words in order
11)use.r.layout
if false, then c++ code is used for collision detection, otherwise R is used
12) fixed.asp
if TRUE, the aspect ratio is fixed. Variable aspect ratio only supported if rot.per==0
13) …
Additional parameters to be passed to text (and strheight,strwidth).

Details
If freq is missing, then words can either be a character vector, or Corpus. If it is a vector and freq is
missing, standard stop words will be removed prior to plotting.

安装:
install.packages(‘wordcloud’)

例子:
wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62))

具体使用例子:

1.直接显示图像

#加载wordcloud包library(wordcloud)#指定字体颜色范围  或者使用R颜色程序包中现成的主题模板 colors=brewer.pal(8,"Dark2")colors=c('red','blue','green','yellow','purple')#读取数据data=read.table("/root/words.xa",header = F)#显示图形wordcloud(data$V2,data$V1,scale=c(5,0.3),min.freq=-Inf,max.words=60,colors=colors,random.order=F,random.color=F,ordered.colors=F)

2.png格式显示

#设置保存图像的目录setwd("/tmp/")#设置保存图像的名字,背景颜色,宽度和高度png(file="wordcloud.png", bg="white",width = 480, height = 480)#加载wordcloud包library(wordcloud)#指定字体颜色范围 或者自定义颜色范围 colors=c('red','blue','green','yellow','purple')colors=brewer.pal(8,"Dark2")#读取数据data=read.table("/root/words.xa",header = F)#显示图形wordcloud(data$V2,data$V1,scale=c(5,0.3),min.freq=-Inf,max.words=60,colors=colors,random.order=F,random.color=F,ordered.colors=F)#结束符dev.off()

例子使用的数据:
6387 尸体
5765 提示
5598 距离
5579 知道
5372 绿竹
4682 打架
4521 挂机
4150 序号
3882 地宫
3266 时候
3202 装备
3183 回来
3177 队长
3148 活力
3125 武当
3104 小号
3032 睡觉
2975 速度
2970 干嘛
2956 五仙
2800 出来
2731 技能
2563 东西
2468 华山
2449 峨眉
2438 个人
2396 没事
2326 师傅
2304 看看
2299 时间
2299 活动
2291 不了
2247 不会
2199 老婆
2092 任务
2079 沙子
2061 游戏
2038 看到
1952 不在
1934 衣服
1909 蜘蛛
1908 盟主
1907 副本
1887 不能
1878 出去
1873 力道
1872 帮派
1818 少林
1813 前锋
1798 武器
1754 升级
1753 大军
1729 朋友
1711 代价
1679 应该
1672 不用
1575 衡山
1571 意思
1561 经验
1558 时装
1534 喜欢
1530 徒弟
1527 龙陵
1523 队伍
1517 垃圾
1509 好像
1505 巨兽
1481 宝石
1458 起来
1445 准备
1445 交易
1438 谢谢
1425 世界
1397 合成
1386 打手
1381 感觉
1380 妹子
1376 百度
1375 助手
1369 吃饭

原创粉丝点击