Skip to content

Instantly share code, notes, and snippets.

@briatte
Last active April 26, 2023 09:43
Show Gist options
  • Save briatte/7b9b70859a4bd5ff7ec6ddda6452e516 to your computer and use it in GitHub Desktop.
Save briatte/7b9b70859a4bd5ff7ec6ddda6452e516 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(readr)
library(stringr)
library(tnet)
library(network) # keep after tnet
read_tsv("keywords.txt")$keywords %>%
str_split(", | and ") %>%
unlist %>%
table %>%
data.frame %>%
arrange(-Freq) %>%
filter(Freq > 1)
e <- read_tsv("keywords.txt")$keywords %>%
str_split(", | and ") %>%
lapply(function(x) {
expand.grid(x, x, w = 1 / length(x), stringsAsFactors = FALSE)
}) %>%
bind_rows
e <- apply(e[, -3], 1, str_sort) %>%
t %>%
data.frame(stringsAsFactors = FALSE) %>%
mutate(w = e$w)
e <- group_by(e, X1, X2) %>%
summarise(w = sum(w)) %>%
filter(X1 != X2)
# undirected network
n <- network(e[, -3], directed = FALSE)
stopifnot(nrow(e) == network.edgecount(n))
set.edge.attribute(n, "weight", e$w)
# weighted degree at alpha = 1
t <- as.edgelist(n, attrname = "weight") %>%
symmetrise_w %>%
as.tnet %>%
degree_w
stopifnot(nrow(t) == network.size(n))
set.vertex.attribute(n, "degree_w", t[, "output" ])
# show only keywords at or above median weighted degree
l <- n %v% "degree_w"
l <- ifelse(l >= median(l), network.vertex.names(n), NA)
stopifnot(length(l) == network.size(n))
set.vertex.attribute(n, "label", l)
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
geom_edges(aes(color = weight)) +
geom_nodes(color = "grey50") +
geom_nodelabel(aes(size = degree_w, label = label),
color = "grey20", label.size = NA) +
scale_size_continuous(range = c(2, 8)) +
scale_color_gradient2(low = "grey25", midpoint = 0.75, high = "black") +
guides(size = FALSE, color = FALSE) +
theme_blank()
@nise
Copy link

nise commented Mar 13, 2023

"n" is not a graph obejct, see line set.edge.attribute(n, "weight", e$w)

@nise
Copy link

nise commented Mar 14, 2023

I've used igraph for the creation of the network.

@briatte
Copy link
Author

briatte commented Apr 26, 2023

The code uses {network}, not {igraph}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment