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 defined by a variable (a string). The aim is to join two types of a dataset in one wordcloud and to differentiate by color. For example, if the type is 'Job' the word appear with the colour blue (#297FD5) and if it's 'Tal' appear with the color grey (#595959).
I tried the following, which gave me the colour defined by the code and not what i want.
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()
Does anyone know how i can change the color for the words depending from a string? Thanks!
CodePudding user response:
If you want to use the color names or hex codes stored in your column df_color you have to use 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()

CodePudding user response:
I think this may be what you are looking for. Since you are using the aes wrapper within ggplot, you need to color the group by changing color to what you want the color to be grouped by. Then you just add scale_color_manual with the colors you want:
#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"))

