colored categories in r wordclouds

烂漫一生 提交于 2019-12-06 11:27:36

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)

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)) ]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!