# get
E(g) # edges
V(g) # vertices
get.edgelist(g) # all edges from to
# select vertice by name
V(g)[V(g)$name=="node1"]
# delete
g <- delete.edges(g, which(E(g)$weight<=0.1) )
g <- delete.vertices(g, which(degree(g)<1) )
# keep specified vertices and all the edges between
g2 <- induced_subgraph(g, 1:7)
# Calculates the length of all the shortest paths from or to the vertices
shortest.paths()
# Convert matrix containg edges and vertices
get.data.frame(xg, what=c("edges"))
# filter graph by edge weight
filtered_edges <- E(g)[weight>0.05]
g1 <- subgraph.edges(g, filtered_edges)
# filter neighbor nodes
# second order neighbors of select_v
g2_list <- make_ego_graph(g, order=2, nodes = select_v, mode = c("all"), mindist = 0)
( xy = (log_b(x y))^b = (log_bx+log_by)^b )
# use use log to tranform product to addition problem
g2 <- subgraph.edges(g2, E(g2)[weight>0]) # remove edge weight ==0
select_v = V(g2)[V(g2)$name==select_cell] # target nodes
E(g2)$weight<- -log(E(g2)$weight)
paths_1 <- shortest.paths(g2, v=V(g2), to=select_v) # use shorest path from verticies to vertice, it is addition problem
paths_2 <- shortest.paths(g2, v=select_v, V(g2))
paths <- rbind(paths_1, t(paths_2))
paths <- exp(-paths) # tranform back the value before log
paths <- paths[paths[, 1] > threshold, ,drop=F]
# g2 only contains second order neighbours where the multiplication of strength > threshold. to plus result of g1 for first order neighbours
g2 <- subgraph(g2, V(g2)[V(g2)$name %in% rownames(paths)])
library(igraph)
g <- graph.tree(10)
plot(g)
is.dag(g) # Ture
## [1] TRUE
g2 <- g + edge(5,1)
plot(g2)
is.dag(g2) # False
## [1] FALSE
Find cliques (complete subgraphs of an undirected graph)
library(igraph)
g <- graph.famous("bull")
cliques(g) # list of cliques
## [[1]]
## + 1/5 vertex, from ae6d467:
## [1] 3
##
## [[2]]
## + 1/5 vertex, from ae6d467:
## [1] 4
##
## [[3]]
## + 1/5 vertex, from ae6d467:
## [1] 2
##
## [[4]]
## + 2/5 vertices, from ae6d467:
## [1] 2 4
##
## [[5]]
## + 2/5 vertices, from ae6d467:
## [1] 2 3
##
## [[6]]
## + 1/5 vertex, from ae6d467:
## [1] 5
##
## [[7]]
## + 2/5 vertices, from ae6d467:
## [1] 3 5
##
## [[8]]
## + 1/5 vertex, from ae6d467:
## [1] 1
##
## [[9]]
## + 2/5 vertices, from ae6d467:
## [1] 1 2
##
## [[10]]
## + 3/5 vertices, from ae6d467:
## [1] 1 2 3
##
## [[11]]
## + 2/5 vertices, from ae6d467:
## [1] 1 3
sapply(cliques(g), length) # clique sizes
## [1] 1 1 1 2 2 1 2 1 2 3 2
largest_cliques(g) # cliques with max number of
## [[1]]
## + 3/5 vertices, from ae6d467:
## [1] 2 1 3
# plot cliques
vcol <- rep("grey80", vcount(g))
vcol[unlist(largest_cliques(g))] <- "gold"
plot(as.undirected(g), vertex.label=V(g)$name, vertex.color=vcol)
g2 <- make_star(10)
V(g2)$label <- 1:10
g2 <- g2 - edge(6)
g2 <- as.undirected(g2)
E(g2)$weight <- 1:8
E(g2)$label <- 1:8
plot(g2)
closeness(g2) # Inverse of the average length of the shortest paths to/from all the other vertices; more weight less closeness
1/closeness(g2,7)/9 # the auto assigned weight of disconnected node (7) to the rest of 9 nodes
library(igraph)
g <- graph.ring(10)
shortest.paths(g)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 0 1 2 3 4 5 4 3 2 1
## [2,] 1 0 1 2 3 4 5 4 3 2
## [3,] 2 1 0 1 2 3 4 5 4 3
## [4,] 3 2 1 0 1 2 3 4 5 4
## [5,] 4 3 2 1 0 1 2 3 4 5
## [6,] 5 4 3 2 1 0 1 2 3 4
## [7,] 4 5 4 3 2 1 0 1 2 3
## [8,] 3 4 5 4 3 2 1 0 1 2
## [9,] 2 3 4 5 4 3 2 1 0 1
## [10,] 1 2 3 4 5 4 3 2 1 0
get.shortest.paths(g, 5)
## $vpath
## $vpath[[1]]
## + 5/10 vertices, from ae85626:
## [1] 5 4 3 2 1
##
## $vpath[[2]]
## + 4/10 vertices, from ae85626:
## [1] 5 4 3 2
##
## $vpath[[3]]
## + 3/10 vertices, from ae85626:
## [1] 5 4 3
##
## $vpath[[4]]
## + 2/10 vertices, from ae85626:
## [1] 5 4
##
## $vpath[[5]]
## + 1/10 vertex, from ae85626:
## [1] 5
##
## $vpath[[6]]
## + 2/10 vertices, from ae85626:
## [1] 5 6
##
## $vpath[[7]]
## + 3/10 vertices, from ae85626:
## [1] 5 6 7
##
## $vpath[[8]]
## + 4/10 vertices, from ae85626:
## [1] 5 6 7 8
##
## $vpath[[9]]
## + 5/10 vertices, from ae85626:
## [1] 5 6 7 8 9
##
## $vpath[[10]]
## + 6/10 vertices, from ae85626:
## [1] 5 4 3 2 1 10
##
##
## $epath
## NULL
##
## $predecessors
## NULL
##
## $inbound_edges
## NULL
get.all.shortest.paths(g, 1, 6:8)
## $res
## $res[[1]]
## + 6/10 vertices, from ae85626:
## [1] 1 10 9 8 7 6
##
## $res[[2]]
## + 6/10 vertices, from ae85626:
## [1] 1 2 3 4 5 6
##
## $res[[3]]
## + 5/10 vertices, from ae85626:
## [1] 1 10 9 8 7
##
## $res[[4]]
## + 4/10 vertices, from ae85626:
## [1] 1 10 9 8
##
##
## $nrgeo
## [1] 1 1 1 1 1 2 1 1 1 1
average.path.length(g)
## [1] 2.777778
## Weighted shortest paths
el <- matrix(nc=3, byrow=TRUE,
c(1,2,0, 1,3,2, 1,4,1, 2,3,0, 2,5,5, 2,6,2, 3,2,1, 3,4,1,
3,7,1, 4,3,0, 4,7,2, 5,6,2, 5,8,8, 6,3,2, 6,7,1, 6,9,1,
6,10,3, 8,6,1, 8,9,1, 9,10,4) )
g2 <- add.edges(graph.empty(10), t(el[,1:2]), weight=el[,3])
shortest.paths(g2, mode="out")
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 0 0 0 1 5 2 1 13 3 5
## [2,] Inf 0 0 1 5 2 1 13 3 5
## [3,] Inf 1 0 1 6 3 1 14 4 6
## [4,] Inf 1 0 0 6 3 1 14 4 6
## [5,] Inf 5 4 5 0 2 3 8 3 5
## [6,] Inf 3 2 3 8 0 1 16 1 3
## [7,] Inf Inf Inf Inf Inf Inf 0 Inf Inf Inf
## [8,] Inf 4 3 4 9 1 2 0 1 4
## [9,] Inf Inf Inf Inf Inf Inf Inf Inf 0 4
## [10,] Inf Inf Inf Inf Inf Inf Inf Inf Inf 0
all_simple_paths(g2, 1, 5)
## [[1]]
## + 3/10 vertices, from ae87c3c:
## [1] 1 2 5
##
## [[2]]
## + 4/10 vertices, from ae87c3c:
## [1] 1 3 2 5
##
## [[3]]
## + 5/10 vertices, from ae87c3c:
## [1] 1 4 3 2 5
g <- make_ring(10)
g <- g + edge(2,4) + edge(3,5)
plot(g)
all_simple_paths(g, 1, 6)
## [[1]]
## + 6/10 vertices, from ae8f0f0:
## [1] 1 2 3 4 5 6
##
## [[2]]
## + 5/10 vertices, from ae8f0f0:
## [1] 1 2 3 5 6
##
## [[3]]
## + 6/10 vertices, from ae8f0f0:
## [1] 1 2 4 3 5 6
##
## [[4]]
## + 5/10 vertices, from ae8f0f0:
## [1] 1 2 4 5 6
##
## [[5]]
## + 6/10 vertices, from ae8f0f0:
## [1] 1 10 9 8 7 6
adjm <- matrix(sample(0:1, 100, replace=TRUE), nc=10)
adjm <- adjm*0.05
g1 <- graph.adjacency(adjm, weighted=T )
plot(g1)
E(g1)$weight
## [1] 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05
## [15] 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05
## [29] 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05
## [43] 0.05 0.05 0.05 0.05
E(g1)$weight <- -log(E(g1)$weight)
E(g1)$weight
## [1] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [8] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [15] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [22] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [29] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [36] 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732 2.995732
## [43] 2.995732 2.995732 2.995732 2.995732
shortest.paths(g1, v=V(g1)[1], to=V(g1)[4])
## [,1]
## [1,] 2.995732
g <- graph.famous("Walther")
ceb <- cluster_edge_betweenness(g)
modularity(ceb) # # modularity measure
## [1] 0.4661811
dendPlot(ceb, mode="hclust")
plot(ceb, g)
ceb <- cluster_fast_greedy(g)
plot(ceb, g)
library(igraph)
df <- data.frame(
A=c("Dublin", "Cork", "Galway") ,
B=c("Cork", "Galway", "Dublin"))
dfg <- graph.data.frame(d=df, directed=FALSE)
V(dfg)$color="red" # set vertex color
E(dfg)$color="blue" # set edge color
plot(dfg, vertex.label=(dfg)$name)
# get the various disconnected components
components(dfg)
## $membership
## Dublin Cork Galway
## 1 1 1
##
## $csize
## [1] 3
##
## $no
## [1] 1
tkplot(dfg) # interactive plot
rglplot(dfg) # 3D plot
g <- barabasi.game(100, directed=F)
layout <- layout.reingold.tilford(g, circular=T)
layout <- layout_as_tree
plot(g, layout=layout)
g <- make_ring(10)
V(g)$name <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
# returns with a list of graphs. b's 2 neighbors
plot(make_ego_graph(g, 2, V(g)[V(g)$name=="b"])[[1]])
https://cran.r-project.org/web/packages/visNetwork/vignettes/Introduction-to-visNetwork.html
require(visNetwork, quietly = TRUE)
vertexes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,1,2), to = c(1,3,3))
visNetwork(vertexes, edges)
Render a visNetwork object from an igraph object
# Not run:
require(igraph)
igraph_network <- graph.famous("Walther")
# get data and plot :
data <- toVisNetworkData(igraph_network)
visNetwork(nodes = data$nodes, edges = data$edges)
# or plot directly
# visIgraph(igraph_network)