在r wordclouds的色的类别 [英] colored categories in r wordclouds

查看:113
本文介绍了在r wordclouds的色的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R中的wordcloud包我想根据数据集中的分类变量为不同的词进行着色。说我的数据如下:

Using the wordcloud package in R I would like to color different words according to a categorical variable in the dataset. Say my data is as follows:

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

code> dput :

And here as a 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)我应该使用不同的软件/包?

Is there a way in wordcloud() to color the names by their group (x, y, z) or should I use different software/packages?

推荐答案

它会根据频率或词序自动从一个颜色列表中选择 ordered.colors

It will automatically choose from a color list based on frequency or by word order if ordered.colors is specified.

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 中来转换为文本,如下所示:

The example above works for independent variables. In a data frame, your color specification will be stored as a factor, and it will have to be converted to text by wrapping it in as.character like this:

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

如果你只有因子而不是颜色列表,可以生成一个并行

If you just have factors and not a list of colors, you can generate a parallel colorlist with a couple of lines.

#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)) ]

这篇关于在r wordclouds的色的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆