我制作了一个示例数据框。我尝试从Projects列创建一个wordcloud。
Hours<-c(2,3,4,2,1,1,3)
Project<-c("a","b","b","a","c","c","c")
Period<-c("2014-11-22","2014-11-23","2014-11-24","2014-11-22", "2014-11-23", "2014-11-23", "2014-11-24")
cd=data.frame(Project,Hours,Period)
这是我的代码:
cd$Project<-as.character(cd$Project)
wordcloud(cd$Project,min.freq=1)
但是我收到以下错误:
Error in strwidth(words[i], cex = size[i], ...) : invalid 'cex' value
In addition: Warning messages:
1: In max(freq) : no non-missing arguments to max; returning -Inf
2: In max(freq) : no non-missing arguments to max; returning -Inf
我做错了什么?
答案 0 :(得分:9)
我认为您错过了freq
参数。您想要创建一个列,指示每个项目发生的频率。因此,我使用count
包中的dplyr
转换了您的数据。
library(dplyr)
library(wordcloud)
cd <- data.frame(Hours = c(2,3,4,2,1,1,3),
Project = c("a","b","b","a","c","c","c"),
Period = c("2014-11-22","2014-11-23","2014-11-24",
"2014-11-22", "2014-11-23", "2014-11-23",
"2014-11-24"),
stringsAsFactors = FALSE)
cd2 <- count(cd, Project)
# Project n
#1 a 2
#2 b 2
#3 c 3
wordcloud(words = cd2$Project, freq = cd2$n, min.freq = 1)
答案 1 :(得分:6)
如果指定了字符列,则该函数会在幕后为您创建语料库和文档术语矩阵。问题是来自tm
pacakge的TermDocumentMatrix函数的默认行为是仅跟踪超过三个字符的单词(同样,它会删除&#34;停用单词&#34;所以像&#这样的值34; a&#34;将被删除)。因此,如果您将样本更改为
Project<-c("aaa","bbb","bbb","aaa","ccc","ccc","ccc")
它会工作得很好。看来没有办法改变发送到TermDocumentMatrix的控制选项。如果您想以与默认wordcloud函数相同的方式自己计算频率,则可以执行
corpus <- Corpus(VectorSource(cd$Project))
corpus <- tm_map(corpus, removePunctuation)
# corpus <- tm_map(corpus, function(x) removeWords(x, stopwords()))
tdm <-TermDocumentMatrix(corpus, control=list(wordLengths=c(1,Inf)))
freq <- slam::row_sums(tdm)
words <- names(freq)
wordcloud(words, freq, min.freq=1)
但是,对于简单的情况,您可以使用table()
tbl <- table(cd$Project)
wordcloud(names(tbl), tbl, min.freq=1)