Skip to content

Instantly share code, notes, and snippets.

@juliasilge
Created May 18, 2019 20:27
Show Gist options
  • Save juliasilge/b2fd4bf16e414c8d48ca8eece7e8f3e8 to your computer and use it in GitHub Desktop.
Save juliasilge/b2fd4bf16e414c8d48ca8eece7e8f3e8 to your computer and use it in GitHub Desktop.
Word co-occurrence network graph
library(tidytext)
library(widyr)
library(igraph)
library(ggraph)
words_cooccur <- tidy_words %>%
group_by(word) %>%
filter(n() > 250) %>%
ungroup() %>%
pairwise_cor(word, Respondent, sort = TRUE, upper = FALSE) %>%
filter(correlation > 0.11)
word_counts <- tidy_words %>%
count(word, sort = TRUE) %>%
filter(word %in% words_cooccur$item1 | word %in% words_cooccur$item2)
words_cooccur %>%
graph_from_data_frame(vertices = word_counts) %>%
ggraph(layout = "nicely") +
geom_edge_link(aes(edge_alpha = correlation),
edge_width = 0.5, show.legend = FALSE) +
geom_node_point(aes(size = n),
alpha = 0.7) +
geom_node_text(aes(label = name), repel = TRUE) +
labs(size = "Number of\nuses") +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment