word cloud -Error in strwidth(words[i], cex = size[i], …) : invalid 'cex' value

孤者浪人 提交于 2019-12-23 05:30:33

问题


I am replicating this word cloud tutorial, but I get:

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

I do not really understand what is going on on each step of the code, but I think the problem might be related to the matrix produced having different rows or columns. This is the code I am using:

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:


The problem is that the default behavior for the TermDocumentMatrix function from the tm pacakge is that is only tracks words that are longer than three characters.

So just add this parameter wordLengths=c(0,Inf) to the control list of TermDocumentMatrix:

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


来源:https://stackoverflow.com/questions/25113372/word-cloud-error-in-strwidthwordsi-cex-sizei-invalid-cex-valu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!