Let me start with the following and fully working code from Introduction to tidytext @ CRAN
library(janeaustenr)
library(dplyr)
library(stringr)
original_books <- austen_books() %>%
group_by(book) %>%
mutate(linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
ignore_case = TRUE)))) %>%
ungroup()
original_books
library(tidytext)
tidy_books <- original_books %>%
unnest_tokens(word, text)
tidy_books
data("stop_words")
cleaned_books <- tidy_books %>%
anti_join(stop_words)
All good so far. I have a tibble with six Jane Austen novels, with the standard junk words removed.
unique(cleaned_books$book)
Which gets me: Sense & Sensibility, Pride & Prejudice, Mansfield Park, Emma, Northanger Abbey, Persuasion.
So if I want to do a standard TF word cloud of all six, no problem. Just like this (color added):
library(wordcloud)
library(RColorBrewer)
dark2 <- brewer.pal(8, "Dark2")
cleaned_books %>%
count(word) %>%
with(wordcloud(word, n, color = dark2, max.words = 100))
Works beautifully. But how do I then do a commonality.cloud() with all six novels, and a comparison.cloud() with the same?
All the data I need is in cleaned_books -- but I can't figure out how to reshape it. Your help appreciated!
Got it. Thanks.
Will leave up in case anyone else has a similar issue.
The above code &
set1 <- brewer.pal(8, "Set1") ## a second color just for other cloud type
library(reshape2)
# title size and scale optional, obviously
cleaned_books %>%
group_by(book) %>%
count(word) %>%
acast(word ~ book, value.var = "n", fill = 0) %>%
comparison.cloud(color = dark2, title.size = 1, scale = c(3, 0.3), random.order = FALSE, max.words = 100)
cleaned_books %>%
group_by(book) %>%
count(word) %>%
acast(word ~ book, value.var = "n", fill = 0) %>%
commonality.cloud(color = set1, title.size = 1, scale = c(3, 0.3), random.order = FALSE, max.words = 100)
That worked out nicely.