I'm trying to make subplots/facets of several wordclouds in a way that's pleasing to the eye.
Problems:
wordcloud
-outputs properlyggplot2
allows for facetting but yields unsatisfactory results
(ugly positioning)I've tried to two ways to create these wordcloud-subplots.
library(dplyr)
library(janeaustenr)
library(tidytext)
df <- austen_books() %>%
unnest_tokens(word, text) %>%
anti_join(stop_words, by = "word") %>%
group_by(book) %>%
count(word) %>%
top_n(100, n)
wordcloud
package and base R:library(wordcloud)
par(mfrow = c(2,2))
png("jane_austen_wordclouds.png")
df %>%
filter(book == "Sense & Sensibility") %>%
with(wordcloud(word, n))
df %>%
filter(book == "Pride & Prejudice") %>%
with(wordcloud(word, n))
df %>%
filter(book == "Mansfield Park") %>%
with(wordcloud(word, n))
df %>%
filter(book == "Emma") %>%
with(wordcloud(word, n))
title( "Jane Austen Word Clouds", outer = TRUE)
dev.off()
Creating:
So somehow it only saves the last subplot. If I don't use png("jane_austen_wordclouds.png")
and dev.off()
and just save the figure straight from RStudio, then I get:
That's also not nice, as it somehow truncates the last three subplots at the top and bottom.
library(ggplot2)
library(ggrepel)
df %>%
filter(book %in% c("Sense & Sensibility", "Pride & Prejudice",
"Mansfield Park", "Emma")) %>%
ggplot(., aes(x = 1, y = 1, size = n, label = word)) +
geom_text_repel(segment.size = 0, segment.alpha = 0) +
scale_size(range = c(2, 15), guide = FALSE) +
theme_void() +
theme(panel.border = element_rect(colour = "black", fill=NA, size=1)) +
facet_wrap(~book) +
labs(title = "Jane Austen Word Clouds")
ggsave("jane_austen_gg.png", width = 11, height = 11)
Which creates:
That looks strangely drawn out along the diagonal. And wordcloud
looks better as it also orients some of the words vertically.
Might there not be a way to insert the pretty wordcloud
figures into ggplot?
I believe you may find useful using gridGraphics and gridExtra to save the plots as objects than draw them again in an arranged manner. I've tested the following code and it worked. Fist save each graph in different objects, as follows:
toPlot<-df %>%
filter(book == "Sense & Sensibility")
wordcloud(toPlot$word, toPlot$n, max.words=100, random.order=FALSE, scale=c(3,0.5))
grid.echo()
a <- grid.grab()
#wordcloud2
toPlot2 <- df %>%
filter(book == "Pride & Prejudice")
wordcloud(toPlot2$word, toPlot2$n, max.words=100, random.order=FALSE, scale=c(3,0.5))
grid.echo()
b <- grid.grab()
#wordcloud3
toPlot3 <- df %>%
filter(book == "Mansfield Park")
wordcloud(toPlot3$word, toPlot3$n, max.words=100, random.order=FALSE, scale=c(3,0.5))
grid.echo()
c <- grid.grab()
#wordcloud4
toPlot4 <- df %>%
filter(book == "Emma")
wordcloud(toPlot4$word, toPlot4$n, max.words=100, random.order=FALSE, scale=c(3,0.5))
grid.echo()
d <- grid.grab()
Then, with the 4 objects, you can arrange them according to a given matrix. See this for further information on layouts: https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html. In the following case, the two wordclouds in the second row have a smaller layout than the upper ones:
grid.newpage()
lay <- rbind(c(1,1,1,2,2,2),
c(1,1,1,2,2,2),
c(1,1,1,2,2,2),
c(1,1,1,2,2,2),
c(3,3,3,4,4,4),
c(3,3,3,4,4,4))
grid.arrange(a,b,c,d, layout_matrix = lay)
The result should be this image:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With