2017-03-09 37 views
0

我想提取該節點周圍的子圖。如何在igraph中提取子圖

library(visNetwork) 
library(igraph) 
library(dplyr) 

g<-data.frame(a=c(LETTERS[1:5], LETTERS[2:6]),b=c(LETTERS[2:6], LETTERS[7:11])) %>% 
     as.matrix %>% graph.edgelist() 

visIgraph(g) %>% 
     visHierarchicalLayout(sortMethod = "directed", 
          direction = "LR",levelSeparation = 300) 

我想提取「d」子開始,像這樣

data.frame(a=c(LETTERS[1:5], LETTERS[2:6]), b=c(LETTERS[2:6], LETTERS[7:11])) %>% 
    as.matrix %>% .[c(4,5,8,9,10),1:2] %>% 
    graph.edgelist() %>% visIgraph() %>% 
    visHierarchicalLayout(sortMethod = "directed", 
          direction = "LR", levelSeparation = 300) 

有沒有辦法得到它?謝謝。

回答

0

你試過了induced.subgraph()函數嗎?

new.vertices<- V(g)[-c(1,2,3,7,8)] 
g.sub <- induced.subgraph(graph = g, new.vertices) 
visIgraph(g.sub) %>% 
    visHierarchicalLayout(sortMethod = "directed", 
         direction = "LR",levelSeparation = 300) 
+0

非常感謝Jonathan,有什麼方法可以找到「D」的子圖路徑而不是1,2,3,7,8? – zaja9031

+0

你可以通過寫內聯'new.vertices < - V(g)[c(4,5,6,9,10,11)]'來做到這一點,但除此之外我不知道函數,因爲您必須指定要包含的所有邊或頂點。您可以嘗試編寫一個貫穿字母表的函數,並選擇頂點> c類型的每個邊。最終你還是要減去頂點列表 – Jonathan

+0

好了解。非常感謝你! – zaja9031