Question

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

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")

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

Was it helpful?

Solution

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)

four-elements

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)) ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top