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

R中两种不同颜色的自定义单词云,r,word-cloud,R,Word Cloud,我试图在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] posi

我试图在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)

完美。谢谢这是非常有帮助和工作!!只是一个跟进,现在我的单词云的形状在视觉上看起来不太好,有没有关于如何改进它的提示?@Vishal-位置部分是随机的。试着运行几次,保存最好的。