【问题标题】:Spaces in wordcloudwordcloud 中的空格
【发布时间】:2011-11-09 18:14:17
【问题描述】:

我目前将 wordle 用于词云的许多艺术用途。我认为 R 的词云可能具有更好的控制力。

1) 如何在词云中保持单词大写? [已解决]

2) 如何在 wordcloud 中将两个单词保持为一个块? (wordle 使用 ~ 操作符来完成此操作,R 的词云只是按原样打印 ~)[例如,在“to”和“be”之间有一个 ~ 我想在词云中留一个空格]

require(wordcloud)

y<-c("the", "the", "the", "tree", "tree", "tree", "tree", "tree", 
"tree", "tree", "tree", "tree", "tree", "Wants", "Wants", "Wants", 
"Wants", "Wants", "Wants", "Wants", "Wants", "Wants", "Wants", 
"Wants", "Wants", "to~be", "to~be", "to~be", "to~be", "to~be", 
"to~be", "to~be", "to~be", "to~be", "to~be", "to~be", "to~be", 
"to~be", "to~be", "to~be", "to~be", "to~be", "to~be", "to~be", 
"to~be", "when", "when", "when", "when", "when", "familiar", "familiar", 
"familiar", "familiar", "familiar", "familiar", "familiar", "familiar", 
"familiar", "familiar", "familiar", "familiar", "familiar", "familiar", 
"familiar", "familiar", "familiar", "familiar", "familiar", "familiar", 
"leggings", "leggings", "leggings", "leggings", "leggings", "leggings", 
"leggings", "leggings", "leggings", "leggings")

wordcloud(names(table(y)), table(y))

【问题讨论】:

  • 您的原始代码是可重现的,我的答案基于此。您的编辑不再可重现,我的回答不再有意义。
  • @Andrie 抱歉,其中一些是自定义函数,我删除了以供将来参考

标签: r word-cloud


【解决方案1】:

你问了两个问题:

  1. 您可以通过为TermDocumentMatrix 指定控制参数来控制(或不)大小写
  2. 毫无疑问,在某处有一个参数可以控制~,但这里有一个简单的解决方法:在绘图前的步骤中使用gsub~ 更改为空白。

一些代码:

corpus <- Corpus(VectorSource(y))
tdm <- TermDocumentMatrix(corpus, control=list(tolower=FALSE)) ## Edit 1

m <- as.matrix(tdm)
v <- sort(rowSums(m), decreasing = TRUE)
d <- data.frame(word = names(v), freq = v)
d$word <- gsub("~", " ", d$word) ## Edit 2

wordcloud(d$word, d$freq)

【讨论】:

  • 这是一个简单的解决方法,我试图使用不必要的 tm 包来解决它。谢谢。
猜你喜欢
  • 2017-10-28
  • 1970-01-01
  • 2021-11-14
  • 2020-04-14
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多