我想绘制两个选定的节点及其所有边缘,而不仅仅是直接连接这两个节点的节点。例如:
library(igraph)
o <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 1
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 1
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 1 0 1 0 1 1 0", header=TRUE)
mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)
我可以使用下面的代码选择g的两个节点,但该图仅包括直接连接它们的边。我想要他们所有人。
g2 <- induced_subgraph(g, c("A","E"))
plot(g2)
如何绘制两个顶点,以及它们边缘的全部?另外,如何为每个顶点选择路径距离?
谢谢!
答案 0 :(得分:0)
library(igraph)
o <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 1
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 1
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 1 0 1 0 1 1 0", header=TRUE)
mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)
plot(g)
# get 1st connections of A and E (their friends)
vertices_input = c("A","E") # specify vertices of interest
ids = as.numeric(E(g)[from(vertices_input)]) # get the ids of the edges from g that correspond to those vertices
g2 = subgraph.edges(g, ids) # keep only those edges from g as a sub-graph g2
plot(g2)
# get up to 2nd connections of A and E (friends of friends)
nms = V(g2)$name # get names of vertices of your sub-graph g2 (main vertices and 1st connections)
ids = as.numeric(E(g)[from(nms)]) # get the ids of the edges from g that correspond to main vertices and 1st connections
g3 = subgraph.edges(g, ids) # keep only those edges from g as a sub-graph g3
plot(g3)