R 语言词云wordcloud

来源:http://blog.chinaunix.net/uid-25135004-id-4311592.html

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()