Skip to content

Instantly share code, notes, and snippets.

@briatte
Last active January 4, 2021 19:31
Show Gist options
  • Save briatte/fb1ca2d10253886ac33ea749aa95f82c to your computer and use it in GitHub Desktop.
Save briatte/fb1ca2d10253886ac33ea749aa95f82c to your computer and use it in GitHub Desktop.
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
library(dplyr)
library(ggnetwork)
library(ggplot2)
library(igraph) # keep after ggnetwork
library(intergraph)
library(readr)
library(stringr)
library(tnet)
# students data
d <- read_tsv("keywords.txt") %>%
arrange(name)
# student names
s <- d$name
# student keywords
k <- d$keywords %>%
str_split(", | and ") %>%
unlist %>%
unique %>%
sort
# 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)
rownames(m) <- s # student names
colnames(m) <- k # student keywords
# 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()
# collapsed one-mode network, using student names
n <- m %*% t(m) %>%
graph_from_adjacency_matrix(mode = "undirected", diag = FALSE, weighted = TRUE)
# weighted degree
V(n)$degree <- as_adjacency_matrix(n, attr = "weight", sparse = FALSE) %>%
degree_w %>%
.[, 3]
V(n)$group <- cluster_louvain(n) %>%
membership %>%
as.character
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