2017-08-07 10 views
0

2つの選択されたノードと、それらの2つのノードを直接接続するノードだけでなく、すべてのエッジをプロットしたいと思います。例:選択された頂点と辺の総数をプロットする方法

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のノードを2つ選択できますが、プロットにはそれらを直接接続するエッジのみが含まれています。私はそれらすべてが欲しい。

g2 <- induced_subgraph(g, c("A","E")) 
plot(g2) 

どのように私は2つの頂点をプロットし、その辺のすべてのですか?また、各頂点のパス距離を選択するにはどうすればよいですか?

ありがとうございます!

+0

「パスの選択」の意味が不明です。しかし、私はあなたの最初の質問であなたを助けることができます。 – AntoniosK

答えて

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) 
+0

こんにちはAntoniosK、これは完璧にうまくいった - ありがとう。友人だけでなく、2つの選択されたノードの友人の友達もプロットする方法を知っていますか? – NBK

+0

はい、それは同じアプローチに従います。主な頂点(AとE)だけを使用するのではなく、A、E、すべての第1の接続(つまり友人)を使用します。私は私の答えを更新します。 – AntoniosK