Alex_
Alex_

Reputation: 187

GGWordcloud with gradient color / transparent words (GGPlot Wordcloud gradient with adjustcolor)

I have created a wordcloud with ggwordcloud, because unfortunately I can't use alternative wordcloud packages. I was able to customize ggwordcloud to my requirements so far, only unfortunately I miss the implementation of a gradient that fades into transparent. So far I have not found a function that allows this.

The following code creates the wordcloud, but only with two colors, but I need a gradient, which goes more and more into the transparent (as in the 2nd code example), so that the smallest words are hidden / transparent.

library(ggwordcloud)
data("love_words_small")
data("love_words")

set.seed(42)
ggplot(
  love_words_small,
  aes(
    label = word, size = speakers,
    color = speakers
  )
) +
  geom_text_wordcloud_area() +
  scale_size_area(max_size = 24) +
  theme_minimal() +
  scale_color_gradient(low = "darkred", high = "red")

The following implementation via the quanteda Wordcloud package has solved my problem so far using adjustcolor:

library(quanteda)
library(quanteda.textplots)
set.seed(10)
dfmat1 <- dfm(corpus_subset(data_corpus_inaugural, President == "Obama"),
              remove = stopwords("english"), remove_punct = TRUE) %>%
   dfm_trim(min_termfreq = 3)
col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))

textplot_wordcloud(dfmat1, adjust = 0.5, random_order = FALSE, 
                   color = col, rotation = FALSE)

Is there any way to transfer this solution to ggwordcloud?

enter image description here

Have many many thanks for any advice!

Upvotes: 2

Views: 321

Answers (1)

Alex_
Alex_

Reputation: 187

I have found the solution myself. It was so obvious...

col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))

library(ggwordcloud)
data("love_words_small")
data("love_words")

set.seed(42)
ggplot(
  love_words_small,
  aes(
    label = word, size = speakers,
    color = speakers
  )
) +
  geom_text_wordcloud_area() +
  scale_size_area(max_size = 24) +
  theme_minimal() +
  scale_color_gradientn(colours = col)

Upvotes: 2

Related Questions