学习 R – 制作短语云图 Word Cloud

利用R制作短语云图需要将短语存为txt文件。for word cloud
#方法1.

#http://www.indiana.edu/~phonlab/R/ImportDataframe.r
#读取txt文件
rm(list=ls())
list.files()
DataSourceFilename = “for word cloud.txt”
Dataframe = RawDataframe = read.table( file=DataSourceFilename, header=TRUE, sep=”\t”, quote=”\””, dec=”.”, row.names=NULL, as.is=FALSE, na.strings=”NA”, colClasses=NA, nrows=0, skip=0, check.names=FALSE, fill=TRUE, strip.white=TRUE, blank.lines.skip=TRUE, comment.char=””, allowEscapes=FALSE, flush=FALSE, stringsAsFactors=TRUE, fileEncoding=””, encoding=”UTF-8″)

#通过UI直接修改数据
fix(Dataframe)

#a<-data.frame(term=Dataframe)
#这里统计短语的频率,将单列数据转换为两列表格,一列是短语,一列是频率
sort(a$term) #排序
sortDataframe<-sort(Dataframe$term)
sumfacsortDataframe<-summary(as.factor(sortDataframe))
#change to data frame
as.data.frame(cbind(Number = names(sumfacsortDataframe),Freq = sumfacsortDataframe), stringsAsFactors=F, row.names = 1:length(sumfacsortDataframe))
DataframeforWC=as.data.frame(cbind(Terms = names(sumfacsortDataframe), Freq = sumfacsortDataframe), stringsAsFactors=F, row.names = 1:length(sumfacsortDataframe))

#改变表格标题行名称
colnames(ss)<-c(“Terms”,”Freq”)

#改变数据类型为integer
DataframeforWC$Freq<-scan(text=DataframeforWC$Freq,what=0L)

#生成word cloud
require(wordcloud)
require(RColorBrewer)
pal2 <- brewer.pal(8,”Dark2″)
png(“wordcloud.png”, width=1500,height=1500)
wordcloud(DataframeforWC$Terms,DataframeforWC$Freq, scale=c(9,.5),min.freq=1, max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2)
dev.off()

#方法2. 使用Excel将数据准备好存为csv格式。for word cloud

require(wordcloud)
require(RColorBrewer)
datain <- read.csv(“./for word cloud.csv”, colClasses=c(“character”, “numeric”),encoding = “UTF-8″)
pal2 <- brewer.pal(8,”Dark2”)
png(“wordcloud.png”, width=1000,height=1000)
wordcloud(datain$term,datain$freq, scale=c(6,.5),min.freq=1, max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2)
dev.off()

发表回复

您的电子邮箱地址不会被公开。