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

时间:2017-07-03 16:46:53

标签: 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]          

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")

1 个答案:

答案 0 :(得分:3)

是。您可以在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)

WordCloud