Last active
January 4, 2021 19:31
-
-
Save briatte/fb1ca2d10253886ac33ea749aa95f82c to your computer and use it in GitHub Desktop.
R code for this note: http://f.briatte.org/r/collapsing-a-bipartite-co-occurrence-network
This file contains 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
name keywords | |
Felicitas Development, Corruption, Housing, Transportation, Religion | |
Lucile Inequality, Migration, Race and Gender, Community/Neighborhood | |
Corentin Inequality, Religion, Migration, Poverty, Gender | |
Charlotte Energy, Environment, Smart Cities, Conflict, Gender | |
Alice Environment, Gender, Migration, Urban Studies | |
Miranda Politics, Gender, Human Rights | |
Margaux Inequality, Urban Studies, Migration, Human Rights | |
Marina Critical Theory, Social Inequality, Race and Gender | |
Cosima Race and Gender and Sexuality, Spatial Inequality, Migration | |
Isabelle Slums, Poverty, Housing, Inequality | |
Elena Race and Gender, Urban Inequality, Immigrant Integration, Collective Action | |
Gabriel Environment, Collective Action, Urban Studies | |
Alexander Transportation, Environmental Justice, Community Empowerment | |
Akhil Tourism, Transportation, Politics | |
Gabriella Multicultural Cohabitation, Environment, Radical Politics and Space Appropriation | |
Mohamed Transport, Inequality, Poverty, Spatial Inequality | |
Lucien Inequality, Poverty, Religious Segregation, Migration |
This file contains 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(dplyr) | |
library(ggnetwork) | |
library(ggplot2) | |
library(igraph) # keep after ggnetwork | |
library(intergraph) | |
library(readr) | |
library(stringr) | |
library(tnet) |
This file contains 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
# students data | |
d <- read_tsv("keywords.txt") %>% | |
arrange(name) |
This file contains 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
# student names | |
s <- d$name |
This file contains 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
# student keywords | |
k <- d$keywords %>% | |
str_split(", | and ") %>% | |
unlist %>% | |
unique %>% | |
sort |
This file contains 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
# incidence matrix | |
m <- sapply(d$keywords, function(x) { | |
str_split(x, ", | and ") %>% unlist | |
}) %>% | |
lapply(function(x) { as.integer(k %in% x) }) %>% | |
unlist %>% | |
matrix(ncol = length(k), byrow = TRUE) |
This file contains 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
rownames(m) <- s # student names | |
colnames(m) <- k # student keywords |
This file contains 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
# flatten out the network to a data frame | |
n <- ggnetwork(m) | |
# single out the primary mode (students) | |
n$student <- n$vertex.names %in% s | |
# plot two-mode network | |
ggplot(n, aes(x, y, xend = xend, yend = yend)) + | |
geom_edges(color = "grey50") + | |
geom_nodelabel(data = n[ !n$student, ], | |
aes(label = vertex.names), | |
color = "grey50", label.size = NA) + | |
geom_nodelabel(data = n[ n$student, ], | |
aes(label = vertex.names), | |
color = "steelblue", fontface = "bold") + | |
theme_blank() |
This file contains 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
# collapsed one-mode network, using student names | |
n <- m %*% t(m) %>% | |
graph_from_adjacency_matrix(mode = "undirected", diag = FALSE, weighted = TRUE) |
This file contains 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
# weighted degree | |
V(n)$degree <- as_adjacency_matrix(n, attr = "weight", sparse = FALSE) %>% | |
degree_w %>% | |
.[, 3] |
This file contains 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
V(n)$group <- cluster_louvain(n) %>% | |
membership %>% | |
as.character |
This file contains 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
ggplot(ggnetwork(n, layout = igraph::with_kk()), | |
aes(x, y, xend = xend, yend = yend)) + | |
geom_edges(aes(alpha = weight)) + | |
geom_nodelabel(aes(label = name, size = degree, color = group)) + | |
scale_alpha_continuous(guide = FALSE) + | |
scale_color_brewer(palette = "Set1", guide = FALSE) + | |
scale_size_continuous(range = c(3, 6), guide = FALSE) + | |
theme_blank() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment