Last active
April 26, 2023 09:43
-
-
Save briatte/7b9b70859a4bd5ff7ec6ddda6452e516 to your computer and use it in GitHub Desktop.
R code for this note: http://f.briatte.org/r/turning-keywords-into-a-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(readr) | |
library(stringr) | |
library(tnet) | |
library(network) # keep after 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
read_tsv("keywords.txt")$keywords %>% | |
str_split(", | and ") %>% | |
unlist %>% | |
table %>% | |
data.frame %>% | |
arrange(-Freq) %>% | |
filter(Freq > 1) |
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
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) |
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
e <- group_by(e, X1, X2) %>% | |
summarise(w = sum(w)) %>% | |
filter(X1 != X2) |
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
# 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" ]) |
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
# 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) |
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(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() |
I've used igraph for the creation of the network.
The code uses {network}
, not {igraph}
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"n" is not a graph obejct, see line
set.edge.attribute(n, "weight", e$w)