Skip to content

Instantly share code, notes, and snippets.

@tonmcg
Forked from sfirke/tidytext_wordclouds.R
Created March 9, 2018 15:53
Show Gist options
  • Save tonmcg/052abe086e06568a9659766793ddc797 to your computer and use it in GitHub Desktop.
Save tonmcg/052abe086e06568a9659766793ddc797 to your computer and use it in GitHub Desktop.
Make wordclouds from a text column in R
library(pacman)
p_load(tidytext, wordcloud, janeaustenr, dplyr)
data("stop_words")
ppdf <- data.frame(prideprejudice, stringsAsFactors = FALSE)
# create a word cloud
create_word_cloud <- function(dat, col_name, exclude = "", max.words = 50, colors = "#034772", ...){
col <- deparse(substitute(col_name))
dat %>%
select_(col) %>%
unnest_tokens_("word", col) %>%
filter(! word %in% exclude) %>%
count(word) %>%
with(wordcloud(word, n, max.words = max.words, colors = colors, ...))
}
create_word_cloud(ppdf, prideprejudice, exclude = stop_words$word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment