Last active
November 16, 2017 02:19
-
-
Save rer145/8f31af53a9a8339dddbef93fd10e86ce to your computer and use it in GitHub Desktop.
Creating a comparison cloud in R with wordcloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gutenbergr) | |
library(tidytext) | |
library(dplyr) | |
library(wordcloud) | |
dracula<-gutenberg_download(345) | |
dracula$gutenberg_id<-NULL | |
dracula<-dracula%>% | |
unnest_tokens(word, text) | |
bing<-get_sentiments('bing') | |
dracula<-inner_join(dracula, bing) | |
words<-dracula%>% | |
group_by(word)%>% | |
summarize(count=n(), sentiment=first(sentiment))%>% | |
arrange(count) | |
# A normal wordcloud plot | |
wordcloud(words$word, words$count, max.words=150) | |
# Reshaping our data to a matrix with reshape2 | |
library(reshape2) | |
matrix<-acast(words, word~sentiment, value.var='count', fill=0) | |
# The comparison cloud | |
comparison.cloud(matrix, colors=c('black', 'orange')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment