Wordcloud 基于 R 中的变量显示颜色

Wordcloud showing colour based on a variable in R

我正在创建一个词云,其中词的大小基于频率,但我希望词的颜色由变量(字符串)定义。目的是将两种类型的数据集连接到一个词云中,并通过颜色进行区分。例如,如果类型为 'Job',则单词显示为蓝色 (#297FD5),如果类型为 'Tal',则显示为灰色 (#595959)。

我尝试了以下方法,它给了我代码定义的颜色,而不是我想要的颜色。

AllWords <- data.frame(word = c("Database", "Database", "Javascript","Javascript", "Java", "Java", "CSS", "CSS"), 
                       scale = c(1.007728, 1.111018, 11.892040, 14.085705, 8.031906, 11.009304, 5.026064, 9.257963 ), 
                       type = c("Job", "Tal","Job", "Tal","Job", "Tal","Job", "Tal"), 
                       df_color = c("#297FD5", "#595959","#297FD5", "#595959","#297FD5", "#595959","#297FD5", "#595959"))

#Packages
library(ggwordcloud)
library(ggplot2)
library(dplyr, quietly = TRUE)
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
library(tidyr, quietly = TRUE)

#Wordcloud
set.seed(42)
ggplot(
  AllWords, 
  aes(
    label = word, 
    size = scale,
    color = df_color
    )) +
  geom_text_wordcloud_area(rm_outside = TRUE, eccentricity = 1) +
  scale_size_area(max_size = 10) +
  theme_minimal()

有谁知道如何根据字符串更改单词的颜色? 谢谢!

如果您想使用存储在您的列 df_color 中的颜色名称或十六进制代码,您必须使用 scale_color_identity:

library(ggwordcloud)
#> Loading required package: ggplot2
library(ggplot2)

set.seed(42)
ggplot(
  AllWords, 
  aes(
    label = word, 
    size = scale,
    color = df_color
  )) +
  geom_text_wordcloud_area(rm_outside = TRUE, eccentricity = 1) +
  scale_size_area(max_size = 10) +
  scale_color_identity() +
  theme_minimal()

我想这可能就是您要找的。由于您在 ggplot 中使用 aes 包装器,您需要通过将 color 更改为您希望颜色分组所依据的颜色来为组着色。然后你只需添加 scale_color_manual 和你想要的颜色:

#Wordcloud
set.seed(42)
ggplot(
  AllWords, 
  aes(
    label = word, 
    size = scale,
    color = word,
  )) +
  geom_text_wordcloud_area(
                           rm_outside = TRUE, 
                           eccentricity = 1) +
  scale_size_area(max_size = 10) +
  theme_minimal()+
  scale_color_manual(values = c("#297FD5", "red", "#704238", "#A64C93"))