词云中的空格问题

5

我目前使用wordle进行词云的艺术化应用。我认为R的词云可能具有更好的控制性。

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

2)如何将两个单词作为一个块保留在词云中?(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
@Andrie 抱歉,其中一些是我为以后参考而删除的自定义函数。 - Tyler Rinker
1个回答

4

你提出了两个问题:

  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)

enter image description here


这是一个很容易解决的问题,我曾试图使用不必要的tm包来解决它。谢谢。 - Tyler Rinker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接