13-Sept-21
Ran through network graph tutorial. I was able to duplicate what they did on the tutorial and change the formatting of the figure slightly, but I am not certain that I would be able to duplicate the method of creating nodes and edges in a new dataset. However, I’m always willing to try!
#Network trial in R
install.packages("visNetwork")
devtools::install_github("sctyner/geomnet")
install.packages("ggplot2")
install.packages("igraph")
install.packages"dplyr"
library(visNetwork)
library(ggplot2)
library(geomnet)
library(igraph)
library (dplyr)
#Load dataset
data(lesmis)
#Nodes
nodes <- as.data.frame(lesmis[2])
colnames(nodes) <- c("id", "label")
#id has to be the same like from and to columns in edges
nodes$id <- nodes$label
#Edges
edges <- as.data.frame(lesmis[1])
colnames(edges) <- c("from", "to", "width")
#Create graph for Louvain
graph <- graph_from_data_frame(edges, directed = FALSE)
#Louvain Comunity Detection
cluster <- cluster_louvain(graph)
cluster_df <- data.frame(as.list(membership(cluster)))
cluster_df <- as.data.frame(t(cluster_df))
cluster_df$label <- rownames(cluster_df)
#Create group column
nodes <- left_join(nodes, cluster_df, by = "label")
colnames(nodes)[3] <- "group"
#Graphs - how do I work the visNodes color function? Doesn't seem to change anything...
visNetwork(nodes, edges, width = "100%") %>%
visIgraphLayout() %>%
visNodes(
shape = "star"
) %>%
visEdges(
shadow = FALSE, color = list(color = "lightseagreen")
) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = T),
selectedBy = "group") %>%
visLayout(randomSeed = 11)
Written on September 13, 2021