Ja123
Ja123

Reputation: 63

R: wordcloud() using RColorBrewers and colorPalette, but without frequency of words

I'm creating a word cloud in R of a list of 103 words.

set.seed(1234)
wordcloud(words=formal_brooke$X1, max.words = 102, scale=c(0.70,.12), random.order=FALSE, colorPalette="Dark2")

I just want to have a mixture of different colors, but my list of words doesn't contain frequencies for the words. Thus all words have the same importance. It doesn't give me a mixture of colors: just black. I can put it on f.e. "red", but then it gives all the words the color red. I just want a nice mixture, but it seems to not do that when I do not implement frequencies to the words. Can anyone help?

Upvotes: 0

Views: 802

Answers (1)

xilliam
xilliam

Reputation: 2259

Here's one approach that uses wordcloud's freq and color arguments. Here you simulate frequencies with freq = rep(1:num_colors,length.out = length(formal_brooke$X1))*100. And the colors are provided with RColorBrewer::brew.pal():

library(wordcloud)
library(RColorBrewer)

# sample data
formal_brooke <- data.frame(X1 = paste0("Word", 1:103))

# specify number of colors
num_colors <- 5

# hex codes for colors                          
pal <- RColorBrewer::brewer.pal(num_colors, "Dark2")

set.seed(1234)
wordcloud(words=formal_brooke$X1,  
          freq = rep(1:num_colors, 
                     length.out = length(formal_brooke$X1))*100, 
          colors = pal,
          max.words = 102, 
          scale=c(0.7,0.12), 
          random.order=TRUE)

enter image description here

And if you would like all the words to be of similar size, set scale = c(1,1)

enter image description here

Upvotes: 2

Related Questions