在R中的Wordcloud中将所有单词大写

Make all words uppercase in Wordcloud in R

创建词云时,最常见的做法是将所有单词都设为小写。但是,我希望 wordclouds 显示大写单词。强制单词为大写后,wordcloud 仍然显示小写单词。有什么想法吗?

可重现代码:

    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)

这将产生以下输出

因为幕后TermDocumentMatrix(clean_corp)正在做TermDocumentMatrix(clean_corp, control = list(tolower = TRUE))。如果将其设置为 TermDocumentMatrix(clean_corp, control = list(tolower = FALSE)),则单词保持大写。或者,您也可以在之后调整矩阵的行名称:rownames(data_m) <- toupper(rownames(data_m))