1

I am using a wordcloud2 package to create wordclouds, but can't seem to find a way to plot them in a grid. I have tried the grid.arrange but it produces an error and doesn't allow me to plot the clouds?

Are there any solutions to do this?

here is my code

    # main wordcloud function
w1 <- wordcloud2(w_virgin, size = 0.8, shape = 'circle',
           rotateRatio = 0.5, minSize = 1)

w2 <- wordcloud2(w_united, size = 0.8, shape = 'circle',
           rotateRatio = 0.5, minSize = 1)

grid.arrange(w1, w2)
qwr
  • 9,525
  • 5
  • 58
  • 102
JimminyCricket
  • 371
  • 3
  • 14
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Give the exact error messages you are receiving. – MrFlick Mar 28 '18 at 16:59
  • @MrFlick thank you for your feedback. I will definitely do that the next time and apologies for this one. I admit i did it in a hurry and did not input all of the information you mentioned. Grateful for your comment though. – JimminyCricket Mar 29 '18 at 07:54

2 Answers2

1

With wordcloud you can just arrange the graphs with the base R par:

par(mfrow=c(1,2)) # for 1 row, 2 cols
wordcloud(d1$word, d1$freq, max.words=100)
wordcloud(d2$word, d2$freq, max.words=100)

If you have a Term-Document Matrix (tdm) you can create the data frame of words and frequencies like this:

library(tm)
v <- sort(rowSums(as.matrix(tdm)),decreasing=TRUE)
d1 <- data.frame(word=names(v),freq=v)

Like MrFlick said though, if you can provide a reproducible example someone may be able to answer more accurately for your problem.

mysteRious
  • 4,102
  • 2
  • 16
  • 36
  • Thank you @mysteRious, I have tried your solution with wordcloud package and it definitely works. It doesn't seem to work with wordcloud2 package which is a different package. Do you think there is another way to do it? – JimminyCricket Mar 29 '18 at 08:01
  • I'm having trouble with that too... the output from `wordcloud2` seems to be `grob`-resistant. I'll let you know if something works. – mysteRious Mar 29 '18 at 18:42
0

The wordcloud function from the wordcloud package plots directly without returning anything, meaning you're pretty much limited to modifying the R output device with par.

If you are using ggplot2, you can use the approximate replacement ggwordcloud from ggwordcloud which outputs a ggplot object that can be arranged as a grob with grid.arrange from gridExtra.

qwr
  • 9,525
  • 5
  • 58
  • 102