【问题标题】:Customised Word Cloud in two different colors in RR中两种不同颜色的自定义词云
【发布时间】:2017-07-03 16:46:53
【问题描述】:

我正在尝试在 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")

【问题讨论】:

    标签: r word-cloud


    【解决方案1】:

    是的。您可以通过在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 - 位置部分是随机的。尝试运行几次并保存最好的。
    猜你喜欢
    • 1970-01-01
    • 2012-07-13
    • 2015-08-11
    • 1970-01-01
    • 2016-06-06
    • 2016-09-29
    • 2015-08-05
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多