Skip to content

Instantly share code, notes, and snippets.

@wesslen
Created May 25, 2018 20:07
Show Gist options
  • Select an option

  • Save wesslen/9c683836a924d1fb9b5d7caa2c9a5844 to your computer and use it in GitHub Desktop.

Select an option

Save wesslen/9c683836a924d1fb9b5d7caa2c9a5844 to your computer and use it in GitHub Desktop.
ggplot wordcloud via quanteda, ggrepel, and tidyverse
library(quanteda); library(ggrepel); library(tidyverse)
ggplotWordcloud <- function(df, maxWords = 50){
corpus(df$text) %>%
dfm(remove_punct = TRUE, remove = stopwords("English")) %>%
topfeatures(n = maxWords) %>%
as.tibble() %>%
rownames_to_column(var = "word") %>%
slice(1:maxWords) %>%
ggplot() +
aes(x = 1, y = 1, size = value, label = word) +
geom_text_repel(segment.size = 0, force = 1) +
scale_size(range = c(2, 10), guide = FALSE) +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
labs(x = '', y = '') +
theme_classic()
}
mutate(fb, text = message) %>%
ggplotWordcloud(maxWords = 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment