首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-从最常用的类别创建wordcloud

R-从最常用的类别创建wordcloud
EN

Stack Overflow用户
提问于 2017-12-11 19:55:12
回答 0查看 321关注 0票数 0

我正在尝试从一些视频中最常用的类别标签创建一个单词云。

一切运行正常,但是在创建文档矩阵时,一些类别被分成单独的单词。这些受影响的类别在单词之间使用"&“符号。

(例如:河与湖,海与岛,海滩与悬壁,...)

如何将这些单词放在一起并正确创建单词云?

代码语言:javascript
复制
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")

#load the text data into docs variable
docs <- Corpus(VectorSource(textos))
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))

#Text Mining. 
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\|")
docs <- tm_map(docs, stripWhitespace)

screenshot of function inspect(docs) showing the words

代码语言:javascript
复制
#Document matrix is a table containing the frequency of the words. 
#Column names are words and row names are documents. 
#The function TermDocumentMatrix() from text mining package can be used as follow

dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)

after applying TermDocumentMatrix. the categories with "& symbol are separated in individual words

代码语言:javascript
复制
#plot the wordcloud

wordcloud(words = d$word, freq = d$freq, scale = c(3,.4), min.freq = 1,
          max.words=Inf, random.order=FALSE, rot.per=0.15, 
          colors=brewer.pal(6, "Dark2"))

result of wordcloud showing the most used categories

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47752408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档