【问题标题】:How separate by grouping variable of word cloud in R?通过在 R 中对词云的变量进行分组如何分开?
【发布时间】:2021-08-21 17:41:56
【问题描述】:

我想制作一个由 autor 变量分组分隔的 wordcloud,我做到了:

# Create a dataframe that save freq of each word

tw <- tw_token %>% count(autor, word, sort=TRUE)

# head of data 
dput(head(tw))
structure(list(autor = c("Mayoredlee", "Mayoredlee", "Elonmusk", 
"Mayoredlee", "Mayoredlee", "Elonmusk"), word = c("sf", "city", 
"tesla", "residents", "housing", "model"), n = c(775L, 320L, 
263L, 260L, 180L, 163L)), row.names = c(NA, 6L), class = "data.frame")


# Make wordcloud 

wordcloud(words = tw$word, freq = tw$n, min.freq = 1,  
          max.words=200, random.order=FALSE, rot.per=0.45,  
          colors=brewer.pal(8, "Dark2"))

wordcloud 的输出

【问题讨论】:

  • 您能否提供带有dput(head(DATA)) 的数据集样本,我们可以复制和粘贴以更好地了解问题和测试解决方案? (见How to make a great R reproducible example)...请告诉我们所需的输出是什么样的!
  • 感谢您的回答,我编辑了帖子

标签: r grouping subplot word-cloud


【解决方案1】:

我认为您可能正在寻找这样的东西:

par(mfrow = c(2, 2)) # output wordclouds on a 2 x 2 grid, adjust if necessary

tw %>%
  split(~ autor) %>%
  purrr::walk(~ with(., wordcloud(words        = word,
                                  freq         = n,
                                  min.freq     = 1,
                                  max.words    = 200,
                                  random.order = FALSE,
                                  rot.per      = 0.45)))

【讨论】:

  • 我试了一下,但我得到了下一个错误:unique.default(x, nmax = nmax) 中的错误:unique() 仅适用于向量
  • 您将问题中的对象名称更改为tw,我更改了答案以反映该更改。您是否在正确的对象上使用了代码?它适用于您提供的对象。
  • 对象名是tw
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
相关资源
最近更新 更多