Chinaunix首页 | 论坛 | 博客
  • 博客访问: 988
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2017-08-24 16:14
文章分类
文章存档

2017年(1)

我的朋友
最近访客

分类: 大数据

2017-08-24 16:15:36

原文地址:R 语言词云wordcloud 作者:xyaxlz

广告:

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、直接显示图像的例子

点击(此处)折叠或打开

  1. #加载wordcloud包
  2. library(wordcloud)
  3. #指定字体颜色范围  或者使用R颜色程序包中现成的主题模板 colors=brewer.pal(8,"Dark2")
  4. colors=c('red','blue','green','yellow','purple')
  5. #读取数据
  6. data=read.table("/root/words.xa",header = F)
  7. #显示图形
  8. 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

点击(此处)折叠或打开

  1. #设置保存图像的目录
  2. setwd("/tmp/")
  3. #设置保存图像的名字,背景颜色,宽度和高度
  4. png(file="wordcloud.png", bg="white",width = 480, height = 480)
  5. #加载wordcloud包
  6. library(wordcloud)
  7. #指定字体颜色范围 或者自定义颜色范围 colors=c('red','blue','green','yellow','purple')
  8. colors=brewer.pal(8,"Dark2")
  9. #读取数据
  10. data=read.table("/root/words.xa",header = F)
  11. #显示图形
  12. 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)
  13. #结束符
  14. dev.off()

显示的词云图像



例子所用的数据words.xa

点击(此处)折叠或打开

  1. 6387 尸体
  2. 5765 提示
  3. 5598 距离
  4. 5579 知道
  5. 5372 绿竹
  6. 4682 打架
  7. 4521 挂机
  8. 4150 序号
  9. 3882 地宫
  10. 3266 时候
  11. 3202 装备
  12. 3183 回来
  13. 3177 队长
  14. 3148 活力
  15. 3125 武当
  16. 3104 小号
  17. 3032 睡觉
  18. 2975 速度
  19. 2970 干嘛
  20. 2956 五仙
  21. 2800 出来
  22. 2731 技能
  23. 2563 东西
  24. 2468 华山
  25. 2449 峨眉
  26. 2438 个人
  27. 2396 没事
  28. 2326 师傅
  29. 2304 看看
  30. 2299 时间
  31. 2299 活动
  32. 2291 不了
  33. 2247 不会
  34. 2199 老婆
  35. 2092 任务
  36. 2079 沙子
  37. 2061 游戏
  38. 2038 看到
  39. 1952 不在
  40. 1934 衣服
  41. 1909 蜘蛛
  42. 1908 盟主
  43. 1907 副本
  44. 1887 不能
  45. 1878 出去
  46. 1873 力道
  47. 1872 帮派
  48. 1818 少林
  49. 1813 前锋
  50. 1798 武器
  51. 1754 升级
  52. 1753 大军
  53. 1729 朋友
  54. 1711 代价
  55. 1679 应该
  56. 1672 不用
  57. 1575 衡山
  58. 1571 意思
  59. 1561 经验
  60. 1558 时装
  61. 1534 喜欢
  62. 1530 徒弟
  63. 1527 龙陵
  64. 1523 队伍
  65. 1517 垃圾
  66. 1509 好像
  67. 1505 巨兽
  68. 1481 宝石
  69. 1458 起来
  70. 1445 准备
  71. 1445 交易
  72. 1438 谢谢
  73. 1425 世界
  74. 1397 合成
  75. 1386 打手
  76. 1381 感觉
  77. 1380 妹子
  78. 1376 百度
  79. 1375 助手
  80. 1369 吃饭



阅读(298) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~