我尝试使用不同的颜色在R中创建一个wordcloud,具体取决于Amount列的符号(蓝色表示正数量,红色表示负数量)。我的数据集(统计数据)如下:
Currency,Amount,Color,ColorCode
AED,-10.50,red,0
AMD,8.20,blue,1
ARS,11.80,blue,1
AUD,-5.60,red,0
BRL,125.80,blue,1
CAD,-26.30,red,0
CHF,89.60,blue,1
CLF,154.80,blue,1
CLP,-100.23,red,0
CNH,14.25,blue,1
CNY,-10.12,red,0
COP,-60.50,red,0
CZK,-33.25,red,0
DKK,-94.60,red,0
DZD,-14.90,red,0
我的代码是:
<pre><code>
set.seed(1234)
wordcloud(words=stats$Currency, freq=stats$Amount, max.words=Inf,
scale=c(9,2), random.order=FALSE, rot.per=0.5, colors=stats$Color)
</code></pre>
生成的wordcloud如下所示: wordcloud example
请注意,货币“AMD”为正(蓝色),但在图像中显示为红色。
谢谢,
答案 0 :(得分:1)
您需要添加ordered.colors = TRUE
library(wordcloud)
set.seed(1234)
wordcloud(words = stats$Currency, freq = stats$Amount, min.freq = -150,
max.words = 200, random.order = FALSE, rot.per = 0.5, scale = c(6,2),
colors = stats$Color, ordered.colors = TRUE)