2

在 RI 中使用 wordcloud 包希望根据数据集中的分类变量为不同的单词着色。说我的数据如下:

  name weight group
1  Aba     10    x
2  Bcd     20    y
3  Cde     30    z
4  Def      5    x

在这里作为dput

dat <- structure(list(name = c("Aba", "Bcd", "Cde", "Def"), weight = c(10, 
    20, 30, 5), group= c("x", "y", "z", "x")), .Names = c("name", 
    "weight", "group"), row.names = c(NA, -4L), class = "data.frame")

wordcloud() 中有没有办法按组(x,y,z)为名称着色,还是我应该使用不同的软件/包?

4

1 回答 1

3

如果指定,它将根据频率或字序自动从颜色列表中选择ordered.colors

name = c("Aba","Bcd","Cde","Def")
weight = c(10,20,30,5)
colorlist = c("red","blue","green","red")

wordcloud(name, weight, colors=colorlist, ordered.colors=TRUE)

四元素

上面的示例适用于自变量。在数据框中,您的颜色规范将被存储为一个因素,并且必须通过将其包装成这样的方式将其转换为文本as.character

wordcloud(df$name, df$weight, colors=as.character(df$color), ordered.colors=TRUE)

如果您只有因子而不是颜色列表,则可以生成colorlist带有几行的平行线。

#general solution for any number of categories
basecolors = rainbow(length(unique(group)))
# solution for known categories
basecolors = c("red","green","blue")

group = c("x","y","z","x")
# find position of group in list of groups, and select that matching color...
colorlist = basecolors[ match(group,unique(group)) ]
于 2013-09-19T20:09:05.387 回答