2

I have built a more complex output via grid.arrange and now I want to include a wordcloud(2). The ggplot wordcloud function is not an option, since it works much slower for my task. But grid.arrange does not allow wordcloud2. However, how can I now save my wordcloud2 output as a grob in order to incorporate it into grid.arrange? The question is analogous to (How to put a wordcloud in a grob?), but with wordcloud2.

Any suggestions?

Alex_
  • 189
  • 8

1 Answers1

1

If you are inclined to consider a different option than the patchwork package may provide a solution for your issue. As a second option I would suggest to have a look at cowplot::as_grob.

library(wordcloud)
#> Loading required package: RColorBrewer
library(ggplot2)
library(patchwork)

words <- c('affectionate', 'ambitious', 'anxious', 'articulate', 'artistic', 'caring', 'contented', 'creative', 'cynical', 'daring', 'dependable', 'easygoing', 'energetic', 'funny', 'generous', 'genuine', 'goodlistener', 'goodtalker', 'happy', 'hardworking', 'humerous', 'impulsive', 'intelligent', 'kind', 'loyal', 'modest', 'optimistic', 'outgoing', 'outrageous', 'passionate', 'perceptive', 'physicallyfit', 'quiet', 'rational', 'respectful', 'romantic', 'shy', 'spiritual', 'spontaneous', 'sweet', 'thoughtful', 'warm')
freqs <- c(134, 53, 0, 5, 0, 247, 0, 78, 0, 0, 134, 178, 79, 344, 63, 65, 257, 0, 109, 113, 0, 0, 107, 51, 199, 24, 67, 232, 0, 109, 24, 28, 29, 2, 105, 70, 0, 35, 64, 156, 66, 45)

p1 <- qplot(1:10, rnorm(10), colour = runif(10))

p1 + ~wordcloud(words, freqs)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks! `Patchwork` is unfortunately not an option, as there is already a larger structure based on `gridArrange`. The hint with `cowplot::as_grob` has steered me in a good direction. Except, I unfortunately can't get the grob cached to use in `grid.arrange`: `Wordcl <- ~wordcloud(words, freqs) grid.newpage() GridWordcloud <- grid.draw(as_grob(Wordcl)) grid.arrange(GridWordcloud)` – Alex_ Jun 28 '21 at 20:54
  • Ah, I noticed that it works with `GridWordcloud <- as_grob(Wordcl)`! But unfortunately it doesn't work with wordcloud2, any ideas? Thanks for saving the evening btw! – Alex_ Jun 28 '21 at 21:05
  • Unfortunately no. :( Not familiar with either wordcloud and wordcloud2. But as far as I get it wordcloud2 is not feasible as it returns an htmlwidget object. I also checked out as_grob. But the results were not really convincing and I ran in the same issue you mentioned. Best S. – stefan Jun 28 '21 at 21:36