Network Visualization

Published

March 20, 2026

Code
library(dplyr)
library(igraph)
library(visNetwork)
library(tibble)

net <- fcolnet2(
  data,
  type = "first",
  waves = list(c(2020, 2026))
)

# remove isolates
noisolate <- rowSums(net$nets[[1]], na.rm = TRUE) > 0
net$nets[[1]] = net$nets[[1]][noisolate, noisolate] 

# build graph
net$graph <- igraph::graph_from_adjacency_matrix(
  net$nets[[1]],
  mode = "directed",
  weighted = NULL,
  diag = FALSE,
  add.colnames = NULL
)

l <- layout_with_fr(net$graph, niter = 1500)

# node data
df_ego <- net$data |>
  filter(uid %in% colnames(net$nets[[1]])) |>
  mutate(
    uni = coalesce(university_22_first, university_24_first, university_25_first)
  ) |>
  arrange(match(uid, colnames(net$nets[[1]])))

# degree for sizing
deg <- igraph::degree(net$graph, mode = "out")

# university colors
uni_colors <- c(
  "UVA" = "red",
  "UVG" = "lemonchiffon",
  "VU"  = "darkgrey",
  "UVT" = "blue",
  "RU"  = "purple",
  "EUR" = "yellow",
  "UU"  = "green",
  "UL"  = "coral"
)

# visNetwork nodes
nodes <- tibble(
  id = V(net$graph)$name,
  label = df_ego$clean_name,
  title = paste0(
    "<b>", df_ego$clean_name, "</b><br>",
    "University: ", df_ego$uni, "<br>",
    "Gender: ", df_ego$gender
  ),
  shape = ifelse(df_ego$gender == "female", "dot", "square"),
  group = df_ego$uni,
  value = 2 + sqrt(deg),
  x = l[, 1] * 100,
  y = l[, 2] * 100
)|>
  arrange(tolower(label), id)

# visNetwork edges
edges <- igraph::as_data_frame(net$graph, what = "edges") |>
  transmute(
    from,
    to,
    width = 0.2
  )

visNetwork(nodes, edges, width = "100%", height = "800px") |>
  visNodes(
    borderWidth = 0.5,
    scaling = list(
      min = 40,
      max = 80
    )
  ) |>
  visEdges(
    smooth = TRUE,
    color = list(color = "rgba(120,120,120,0.5)")
  ) |>
  visPhysics(
    solver = "barnesHut",
    barnesHut = list(
      gravitationalConstant = -4000,
      centralGravity = 0.10,
      avoidOverlap = 0.4,
      springLength = 300,
      springConstant = 0.05
    )
  ) |>
  visInteraction(
    dragNodes = FALSE,
    dragView = TRUE,
    zoomView = TRUE,
    hover = TRUE
  ) |>
  visOptions(
    highlightNearest = list(
      enabled = TRUE,
      degree = 1,
      hover = FALSE
    ),
    nodesIdSelection = list(
      enabled = TRUE,
      useLabels = TRUE,
      main = "Select author"
    )
  ) |>
  visLegend(
    position = "right",
    width = 0.2
  )
Back to top