我正在尝试在 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)
关于r - R中两种不同颜色的自定义词云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44890569/