How to set number of colours from custom palette wordcloud in R uses

16 Views Asked by At

I'm trying to build a wordcloud of some survey answers in R that is coloured by frequency, but using a custom colour palette I've been asked to use. The palette has 8 colours, but no matter what I try, it only seems to use 5 or 6 of them. I'd like to be able to play with how many I use, to see the best result, (e.g. set it at 4 or 7) and the problem has gotten beyond my own meagre skills. Can anyone tell me what the best solution would be?

This is what I started with

library(tidytext)
library(tidyverse)
library(wordcloud)

#This is the custom palette I want to use
Mycols <- c(
  "#313d48",
  "#46d9e8",
  "#ffa900",
  "#ff5c00",
  "#00ba5a",
  "#ab81bf",
  "#c6006f",
  "#0a28c8")



#Dummy data, but not wildly different to my actual data
answers <- billboard %>%
  select(track) %>%
  unnest_tokens(word, track) %>%
  anti_join(stop_words) %>%
  filter(!is.na(word)) %>%
  group_by(word) %>%
  count() %>%
  arrange(desc(n))

set.seed(559923)
wordcloud(words = answers$word, 
          freq = answers$n, 
          random.order = FALSE,
          min.freq = 1,
          rot.per = 0.35, 
          colors = Mycols)

I've found examples that use RColorBrewer to set the number of colours used, but they don't help as I have to use the custom palette. Other answers I've found are on setting the colours based on another variable, which isn't what I want to do, so now I'm a bit stumped. All help gratefully received.

0

There are 0 best solutions below