word cloud-strwidth中的错误(words [i],cex = size [i],...):无效' cex'值

时间:2014-08-04 07:09:18

标签: r word-cloud

我正在复制this文字云教程,但我得到了:

strwidth错误(words [i],cex = size [i],...):无效的'cex'值 另外:警告信息: 1:在max(freq)中:max没有非缺失参数;返回-Inf 2:在max(freq)中:max没有非缺失参数;返回-Inf

我真的不明白代码的每一步发生了什么,但我认为问题可能与生成的具有不同行或列的矩阵有关。这是我正在使用的代码:

install.packages(c("devtools", "rjson", "bit64", "httr"))

library(devtools)
install_github("twitteR", username="geoffjentry")
library(twitteR)

##
api_key= "xxxxxx"
api_secret= "xxxxxx"
access_token="xxxxxxxxxxxx"
access_token_secret= "xxxxxx"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)

searchTwitter("amlo")

library(twitteR)
install.packages("tm")
library(tm)
install.packages("wordcloud")
library(wordcloud)
library(RColorBrewer)

mh370 <- searchTwitter("#PrayForMH370", since = "2014-03-08", until = "2014-03-20", n =             1000)
mh370_text = sapply(mh370, function(x) x$getText())
mh370_corpus = Corpus(VectorSource(mh370_text))

tdm = TermDocumentMatrix(
  mh370_corpus,
  control = list(
    removePunctuation = TRUE,
    stopwords = c("prayformh370", "prayformh", stopwords("english")),
    removeNumbers = TRUE, tolower = TRUE)
)

m = as.matrix(tdm)
# get word counts in decreasing order
word_freqs = sort(rowSums(m), decreasing = TRUE) 
# create a data frame with words and their frequencies
dm = data.frame(word = names(word_freqs), freq = word_freqs)
wordcloud(dm$word, dm$freq, random.order = FALSE, colors = brewer.pal(8, "Dark2"))

1 个答案:

答案 0 :(得分:0)

问题是来自tm pacakge的TermDocumentMatrix函数的默认行为是仅跟踪长于三个字符的单词。

所以只需将此参数 wordLengths = c(0,Inf)添加到TermDocumentMatrix的控制列表中:

tdm = TermDocumentMatrix(
     mh370_corpus,
     control = list(
     wordLengths=c(0,Inf),
     removePunctuation = TRUE,
     stopwords = c("prayformh370", "prayformh", stopwords("english")),
     removeNumbers = TRUE, tolower = TRUE) )