Created
May 25, 2018 20:07
-
-
Save wesslen/9c683836a924d1fb9b5d7caa2c9a5844 to your computer and use it in GitHub Desktop.
ggplot wordcloud via quanteda, ggrepel, and tidyverse
This file contains hidden or 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(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