-
-
Save merico34/b35aeeb291f18bd949ce87bcf1dc9993 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 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
| 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 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(dplyr) | |
| library(ggnetwork) | |
| library(ggplot2) | |
| library(igraph) # keep after ggnetwork | |
| library(intergraph) | |
| library(readr) | |
| library(stringr) | |
| library(tnet) |
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
| # students data | |
| d <- read_tsv("keywords.txt") %>% | |
| arrange(name) |
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
| # student names | |
| s <- d$name |
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
| # student keywords | |
| k <- d$keywords %>% | |
| str_split(", | and ") %>% | |
| unlist %>% | |
| unique %>% | |
| sort |
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
| # 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 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
| rownames(m) <- s # student names | |
| colnames(m) <- k # student keywords |
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
| # 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 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
| # collapsed one-mode network, using student names | |
| n <- m %*% t(m) %>% | |
| graph_from_adjacency_matrix(mode = "undirected", diag = FALSE, weighted = TRUE) |
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
| # weighted degree | |
| V(n)degree <- as_adjacency_matrix(n, attr = "weight", sparse = FALSE) %>% | |
| degree_w %>% | |
| .[, 3] |
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
| V(n)$group <- cluster_louvain(n) %>% | |
| membership %>% | |
| as.character |
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
| ggplot(ggnetwork(n, layout = "kamadakawai"), | |
| aes(x, y, xend = xend, yend = yend)) + | |
| geom_edges(aes(alpha = weight)) + | |
| geom_nodelabel(aes(label = vertex.names, 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