Skip to content

Instantly share code, notes, and snippets.

@rer145
Last active November 16, 2017 02:19
Show Gist options
  • Save rer145/8f31af53a9a8339dddbef93fd10e86ce to your computer and use it in GitHub Desktop.
Save rer145/8f31af53a9a8339dddbef93fd10e86ce to your computer and use it in GitHub Desktop.
Creating a comparison cloud in R with wordcloud
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