R:在wordcloud中传播单词

R: Spreading the words in wordcloud

我在使用 wordcloud 功能时很难宣传。

data = tibble(Day = c("January", "February", "March" , "April", "May", "June", "July", "August", "Semptember", "October", "November", "December"),
              Freq = c(1294, 1073, 1071, 1019, 938, 912, 703, 680, 543, 201, 190, 343))

set.seed(10)
wordcloud(words = data$Day, freq = data$Freq, min.freq = 1, 
          random.order=T, scale=c(3,.5), rot.per = 0)

我尝试使用 ggsave 函数保存输出,但这是我得到的:

期望的输出:

我在 wordcloud 中找不到执行此操作的方法,但 wordcloud2 提供了更大的灵活性。在另一个保存为图像文件的 SO 问题的帮助下,我设法拼凑了这个。



#packages enable saving to png or pdf via html, see link at end of answer
library(webshot)
webshot::install_phantomjs()
library("htmlwidgets")


library(tibble)
library(wordcloud2)

data = tibble(Day = c("January", "February", "March" , "April", "May", "June", "July", "August", "Semptember", "October", "November", "December"),
              Freq = c(1294, 1073, 1071, 1019, 938, 912, 703, 680, 543, 201, 190, 343))


set.seed(10)

# control appearance with wordcloud2 arguments. The padding between words is controlled by `gridsize`.
# You have to play around with `size`, `gridSize` and the image size

eg <- wordcloud2(data, size = 0.4, rotateRatio = 0, color = "black", gridSize = 75)


# save as html
saveWidget(wc,"wc.html", selfcontained = F)


# and then as image:png
webshot("wc.html","wc.png", delay = 5, vwidth = 480, vheight = 480)


要将图像保存到文件,请参阅:How to Save the wordcloud in R

你最终得到:

reprex package (v0.3.0)

于 2020-05-18 创建