R中两种不同颜色的自定义词云

Customised Word Cloud in two different colors in R

我想在 R 中创建一个词云,其中我有一个包含正面和负面词的矩阵,但是,我想用两种不同的颜色(比如绿色和红色)显示正面和负面词。有人可以帮我解决这个问题吗?谢谢!

library(qdap)

x1 = x[a0]    

pol = polarity(x1)        
wc = pol$all[,2]          
val = pol$all[,3]         
p  = pol$all[,4]          
n  = pol$all[,5]          

positive_words = unique(setdiff(unlist(p),"-"))  # Positive words list
negative_words = unique(setdiff(unlist(n),"-"))  # Negative words list
total_words1 =cbind(positive_words,negative_words)

pos.tdm = dtm[,which(colnames(dtm) %in% total_words1)]
m = as.matrix(pos.tdm)
v1 = sort(colSums(m), decreasing = TRUE)

windows() # opens new image window
wordcloud(names(v1), v1, scale=c(4,1),1, max.words=100,colors=brewer.pal(8, "Dark2"))
title(sub = "Words - Wordcloud")

是的。您可以通过在 colors 中列出每个单词然后使用 ordered.colors=TRUE 来为每个单词选择颜色。我给出了一个仅包含红色和绿色单词的简单示例,但您可以根据单词的出现频率来改变红色和绿色的深浅。

Pos = read.table(text="Word    Count
Great       10
Good        25 
Fabulous    7",
header=TRUE, stringsAsFactors = TRUE)

Neg = read.table(text="Word    Count
Bad         23
Stinks      5
Terrible    15",
header=TRUE, stringsAsFactors = TRUE)

AllWords = rbind(Pos, Neg)
Colors = c(rep("green", nrow(Pos)), rep("red", nrow(Neg)))

wordcloud(AllWords $Word, AllWords $Count, 
        colors=Colors, ordered.colors=TRUE)