R - wordcloud 包没有正确应用给定的颜色

R - wordcloud package not applying given colors correctly

我想从 wordcloud 应用以下 vector of colors:

创建一个 png
colorVector <- c("#797978", "#797978", "#797978", "#797978", "#797978", "#797978", 
"#FF6633", "#FF6633", "#797978", "#797978", "#797978", "#797978", 
"#00FF99", "#797978", "#797978", "#797978", "#797978", "#797978", 
"#00FF99", "#797978", "#797978", "#797978", "#797978", "#797978", 
"#797978", "#797978", "#797978", "#797978", "#00FF99", "#FF6633"
)

# Create wordcloud and save the image in png format
png("wcMM.png", width=7.5, height=5.5, units="cm", res=140, bg = "white")
wordcloud(words = topfeaturesF$keyName, 
          freq = topfeaturesF$value.x, 
          scale=c(3,.9),
          min.freq=2,max.words=50, 
          random.order=T, rot.per=.25, 
          colors=topfeaturesF$color, 
          family ="Verdana")
dev.off()

虽然词云变色了,但它们(颜色)并没有应用到正确的词上,如下图所示,属性 colors 似乎表现得很奇怪,如vector 只定义了 3 个橙子和 3 个绿色,但是 1 个绿色和 7 个橙子 "painted":

来自 wordcloud 包文档:

colors = color words from least to most frequent

我是否误解了颜色属性的工作原理?

数据框:

topfeaturesF <- structure(list(keyName = structure(c(26L, 20L, 4L, 18L, 8L, 12L, 
    22L, 7L, 21L, 11L, 17L, 3L, 29L, 14L, 30L, 16L, 28L, 27L, 2L, 
    25L, 6L, 24L, 9L, 1L, 13L, 19L, 5L, 23L, 10L, 15L), .Label = c("bas", 
    "corporation", "deutsch", "deutschland", "digital", "erst", "euro", 
    "fil", "gibt", "gmbh", "jahr", "kund", "lte", "mobil", "mod", 
    "monat", "netz", "neu", "nutz", "o2", "plus", "program", "prozent", 
    "schon", "tarif", "telefonica", "telekom", "unternehm", "windows", 
    "wurd"), class = "factor"), id = structure(c(26L, 20L, 4L, 18L, 
    8L, 12L, 22L, 7L, 21L, 11L, 17L, 3L, 29L, 14L, 30L, 16L, 28L, 
    27L, 2L, 25L, 6L, 24L, 9L, 1L, 13L, 19L, 5L, 23L, 10L, 15L), .Label = c("bas42461", 
    "corporation42461", "deutsch42461", "deutschland42461", "digital42461", 
    "erst42461", "euro42461", "fil42461", "gibt42461", "gmbh42461", 
    "jahr42461", "kund42461", "lte42461", "mobil42461", "mod42461", 
    "monat42461", "netz42461", "neu42461", "nutz42461", "o242461", 
    "plus42461", "program42461", "prozent42461", "schon42461", "tarif42461", 
    "telefonica42461", "telekom42461", "unternehm42461", "windows42461", 
    "wurd42461"), class = "factor"), value.x = c(2907, 1400, 1239, 
    998, 810, 701, 689, 685, 615, 592, 585, 543, 540, 532, 523, 504, 
    472, 472, 468, 467, 464, 460, 432, 392, 373, 372, 368, 347, 338, 
    336), date = c(42461, 42461, 42461, 42461, 42461, 42461, 42461, 
    42461, 42461, 42461, 42461, 42461, 42461, 42461, 42461, 42461, 
    42461, 42461, 42461, 42461, 42461, 42461, 42461, 42461, 42461, 
    42461, 42461, 42461, 42461, 42461), value.y = c(2430L, 1576L, 
    780L, 886L, 883L, 648L, 812L, 787L, 614L, 566L, 567L, 355L, 301L, 
    328L, 552L, 515L, 323L, 387L, 190L, 426L, 294L, 354L, 335L, 342L, 
    254L, 241L, 265L, 212L, 203L, NA), diff = c(0.196296296296296, 
    -0.111675126903553, 0.588461538461538, 0.126410835214447, -0.0826727066817667, 
    0.0817901234567902, -0.151477832512315, -0.129606099110546, 0.00162866449511401, 
    0.0459363957597174, 0.0317460317460319, 0.529577464788732, 0.794019933554817, 
    0.621951219512195, -0.052536231884058, -0.0213592233009708, 0.461300309597523, 
    0.219638242894057, 1.46315789473684, 0.096244131455399, 0.578231292517007, 
    0.299435028248588, 0.28955223880597, 0.146198830409357, 0.468503937007874, 
    0.543568464730291, 0.388679245283019, 0.636792452830189, 0.665024630541872, 
    NA), color = c("#797978", "#797978", "#797978", "#797978", "#797978", 
    "#797978", "#FF6633", "#FF6633", "#797978", "#797978", "#797978", 
    "#797978", "#00FF99", "#797978", "#797978", "#797978", "#797978", 
    "#797978", "#00FF99", "#797978", "#797978", "#797978", "#797978", 
    "#797978", "#797978", "#797978", "#797978", "#797978", "#00FF99", 
    "#FF6633")), .Names = c("keyName", "id", "value.x", "date", "value.y", 
    "diff", "color"), row.names = c(26L, 20L, 4L, 18L, 8L, 12L, 22L, 
    7L, 21L, 11L, 17L, 3L, 29L, 14L, 30L, 16L, 28L, 27L, 2L, 25L, 
    6L, 24L, 9L, 1L, 13L, 19L, 5L, 23L, 10L, 15L), class = "data.frame")

您可能正在寻找 ordered.colors 参数:

library(wordcloud)
wordcloud(words = topfeaturesF$keyName, 
          freq = topfeaturesF$value.x, 
          scale=c(3,.9),
          min.freq=2,max.words=50, 
          random.order=T, rot.per=.25, 
          colors=topfeaturesF$color,
          ordered.colors=TRUE,
          family ="Verdana")