Wordcloud 根据 R 中的连续元数据显示颜色 [英] Wordcloud showing colour based on continous metadata in R

查看:26
本文介绍了Wordcloud 根据 R 中的连续元数据显示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 wordcloud,其中单词的大小基于频率,但我希望将单词的颜色映射到第三个变量(压力,即与每个单词相关的压力量,数值或连续变量).

I'm creating a wordcloud in which the size of the words is based on frequency, but i want the colour of the words to be mapped to a third variable (stress, which is the amount of stress associated with each word, a numerical or continuous variable).

我尝试了以下方法,它只给了我两种不同的颜色(黄色和紫色)​​,而我想要更光滑的颜色.我想要一些颜色范围,例如从绿色到红色的调色板.

I tried the following, which gave me only two different colours (yellow and purple) while i want something more smooth. I would like some color range like a palette that goes from green to red for example.

df = data.frame(word = c("calling", "meeting", "conference", "contract", "negotiation", "email"),
n = c(20, 12, 4, 8, 10, 43),
stress = c(23, 30, 15, 40, 35, 15))
df = tbl_df(df) 
wordcloud(words = df$word, freq = df$n, col = df$stress)

有谁知道如何处理这种连续的元数据并在压力增加时为单词获得一些平滑变化的颜色?谢谢!

Does anyone know how to deal with this continous metadata and get some smoothly changing colour for the words when stress goes up? Thanks!

推荐答案

这是一个潜在的解决方案.您想将 wordcloud2 包用于您的任务.然后,我想你可以解决你的问题.由于我不知道你的真实数据,我创建了一个示例数据来演示一个原型.

Here is a potential solution. You want to use the wordcloud2 package for your task. Then, you can solve your issue, I suppose. Since I do not know your real data, I created a sample data to demonstrate a prototype.

如果您有很多单词,我不确定添加具有连续变量(应力)的颜色是否是一个好主意.您可以做的一件事是使用 cut() 创建一个新的组变量.通过这种方式,您可以减少在图形中使用的颜色数量.在这里,我创建了一个名为 color 的新列,其中包含来自 viridis 包的五种颜色.

If you have many words, I am not sure if adding colors with a continuous variable (stress) is a good idea. One thing you could do is to create a new group variable using cut(). In this way, you can reduce the numbers of colors you would use in your graphics. Here, I created a new column called color with five colors from the viridis package.

当您使用 wordcloud2() 时,您只需提供两件事.一个是数据,另一个是颜色.字体大小反映了单词的频率,无需指定.

When you use wordcloud2(), you have only two things to supply. One is data and the other is color. Font size reflects frequency of the words without specifying it.

mydf = data.frame(word = c("calling", "meeting", "conference", "contract", "negotiation",
                           "email", "friends", "chat", "text", "deal",
                           "business", "promotion", "discount", "users", "family"),
                  n = c(20, 12, 4, 8, 10, 43, 33, 5, 47, 28, 12, 9, 50, 31, 22),
                  stress = c(23, 30, 15, 40, 35, 15, 30, 18, 10, 5, 29, 38, 45, 8, 3))


          word  n stress
1      calling 20     23
2      meeting 12     30
3   conference  4     15
4     contract  8     40
5  negotiation 10     35
6        email 43     15
7      friends 33     30
8         chat  5     18
9         text 47     10
10        deal 28      5
11    business 12     29
12   promotion  9     38
13    discount 50     45
14       users 31      8
15      family 22      3

library(dplyr)
library(wordcloud2)
library(viridis)

mutate(mydf, color = cut(stress, breaks = c(0, 10, 20, 30, 40, Inf),
             labels = c("#FDE725FF", "#73D055FF", "#1F968BFF",
                        "#2D708EFF", "#481567FF"),
             include.lowest = TRUE)) -> temp

wordcloud2(data = temp, color = temp$color)

这篇关于Wordcloud 根据 R 中的连续元数据显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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