首页 > 解决方案 > 如何在 r 中创建和打印 wordcloud

问题描述

不知道我错过了什么,但是当我运行这个 wordcloud 示例时,没有绘制任何内容并且 x = NULL。

图书馆都是可用的。

abc<-data.frame(X=LETTERS[1:26],x=sample(1:26))

x = wordcloud(abc$X,abc$x,scale = c(5,.5),min.freq = 2,colors = brewer.pal(10,"Paired"))

标签: r

解决方案


您可以使用 quanteda 包。基于他们的文档的示例https://quanteda.io/reference/textplot_wordcloud.html

library(quanteda)
#convert readLines input to a token object
tok <- tokens(unlist(sample.lines))

#convert the token object to a frequency matrix
dfmat1 <- dfm(tok,
              remove = stopwords("english"), #remove stopwords (if wanted)
              remove_punct = TRUE, #remove punctuation (if wanted)
              tolower = T, #(change all to lowercase (if wanted)
              removeNumbers = TRUE) %>%  #remove numbers (if wanted)
   dfm_trim(min_termfreq = 4) #eliminate frequencies lower than 4 (if wanted)



# basic wordcloud
textplot_wordcloud(dfmat1)

# plot in colors with some additional options
textplot_wordcloud(dfmat1, rotation = 0.25, 
                   color = rev(RColorBrewer::brewer.pal(10, "RdBu")))

# other display options
col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))
textplot_wordcloud(dfmat1, adjust = 0.5, random_order = FALSE, 
                   color = col, rotation = FALSE)

推荐阅读