Make all words uppercase in Wordcloud in R

老子叫甜甜 提交于 2019-12-23 21:22:11

问题


When creating Wordclouds it is most common to make all the words lowercase. However, I want the wordclouds to display the words uppercase. After forcing the words to be uppercase the wordcloud still display lowercase words. Any ideas why?

Reproducable code:

    library(tm)
    library(wordcloud)

data <- data.frame(text = c("Creativity is the art of being ‘productive’ by using
          the available resources in a skillful manner. 
          Scientifically speaking, creativity is part of
          our consciousness and we can be creative –
          if we know – ’what goes on in our mind during
          the process of creation’.
          Let us now look at 6 examples of creativity which blows the mind."))

text <- paste(data$text, collapse = " ")

# I am using toupper() to force the words to become uppercase.
text <- toupper(text)

source <- VectorSource(text)
corpus <- VCorpus(source, list(language = "en"))

# This is my function for cleaning the text                  
clean_corpus <- function(corpus){
             corpus <- tm_map(corpus, removePunctuation)
             corpus <- tm_map(corpus, removeNumbers)
             corpus <- tm_map(corpus, stripWhitespace)
             corpus <- tm_map(corpus, removeWords, c(stopwords("en")))
             return(corpus)
}   

clean_corp <- clean_corpus(corpus)
data_tdm <- TermDocumentMatrix(clean_corp)
data_m <- as.matrix(data_tdm)

commonality.cloud(data_m, colors = c("#224768", "#ffc000"), max.words = 50)

This produces to following output


回答1:


It's because behind the scenes TermDocumentMatrix(clean_corp) is doing TermDocumentMatrix(clean_corp, control = list(tolower = TRUE)). If you set it to TermDocumentMatrix(clean_corp, control = list(tolower = FALSE)), then the words stay uppercase. Alternatively, you can also adjust the row names of your matrix afterwards: rownames(data_m) <- toupper(rownames(data_m)).



来源:https://stackoverflow.com/questions/40653728/make-all-words-uppercase-in-wordcloud-in-r

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