# the network visualisation tools
library(network)
library(GGally)
library(sna)
# our usual data wrangling tools
library(here)
library(tidyverse)
library(magrittr)
# the data are stored as a zipped tab-separated
# value file. we can read it directly without
# manually unzipping...
swow <- here("network","swow.csv.zip") %>%
read_tsv()
# function that takes a set of user-defined words
# and returns a subnetwork of related words and the
# connections between them
make_net <- function(words, edge_threshold) {
# a word is deemed "nearby" to the listed words if
# it appears as a response to one of the words (a
# forward association from the word) or elicts the
# word as a response (backward association). we
# impose a threshold where these associations only
# count if they occur often enough...
near_words <- swow %>%
filter(cue %in% words | response %in% words) %>%
filter(R1.Strength > edge_threshold) %$%
union(cue, response)
# now construct the complete semantic network among
# all words within this set of "nearby" words, again
# imposing threshold
edges <- swow %>%
filter(cue %in% near_words & response %in% near_words) %>%
filter(R1.Strength > edge_threshold)
# take the network data and recode this as a
# formal "network" data structure
net <- edges %>%
select(cue, response) %>%
as.network(matrix.type = "edgelist")
# add vertex attribute indicating whether it was a
# seed word
names <- network.vertex.names(net)
kind = ifelse(names %in% words, "seed", "other")
net %v% "kind" <- kind
return(net)
}
# a function to draw a pretty pictue
show_net <- function(net, label_threshold) {
# work out which nodes to label: specifically,
# only those with sufficiently high degree-centrality
# (i.e. lots of connections)
labels <- network.vertex.names(net)
centrality <- degree(net)
labels <- labels[centrality > label_threshold]
# construct a picture using the ggnet2 function
pic <- net %>%
ggnet2(
alpha = .8,             # transparency of nodes
size = "degree",        # size of note represents degree
size.min = 1,           # smallest node size allowed
color = "kind",
palette = c("seed" = "gold", "other" = "lightblue"),
edge.color = "grey85",  # colour of edges
label = labels          # the labels
)
plot(pic) # draw it!
}
# simple wrapper function
visualise <- function(words, thresholds) {
words %>%                                             # pipe the words...
make_net(edge_threshold = thresholds["edge"]) %>%   # to make a network...
show_net(label_threshold = thresholds["label"])     # ... and plot it
}
# example:
visualise(
words = c("man","woman"),
thresholds = c("edge" = .04, "label" = 4)
)
# the data are stored as a zipped tab-separated
# value file. we can read it directly without
# manually unzipping...
swow <- here("network","swow.csv.zip") %>%
read_tsv()
