2017-02-14 67 views
0

我想使用GGally或ggnetwork繪製一個網絡對象,我希望能夠生成一個佈局,其中節點按頂點屬性分組。我花了一些時間尋找一種方法來做到這一點,但還沒有弄清楚。節點是否可以按照屬性佈局進行分組,使得所有具有屬性「a」的節點位於集羣中,屬性「b」的節點位於集羣中等。如何通過ggnet2或ggnetwork中的頂點屬性對節點進行分組

在此先感謝。

這裏有兩個例子:

library (GGally) 
library (ggnetwork) 
library (ggplot2) 
library (sna) 
library (network) 

#make a random network with some vertex attributes 
abc<-as.network(rgraph(20,1)) 
abc %v% "kinds" <- letters[1:3] 
abc %v% "model" <- LETTERS[12:18] 

#plot the network using ggnet2 in library (GGally) 
#I want to somehow group the nodes together by a vertex attribute. 
#Here I have tried to group nodes by "kinds." How to do this?? 
ggnet2(abc, 
     size="degree", size.cut=3, 
     color = "kinds", 
     group = "kinds") 


#and here is an example using library (ggnetwork) 

#set degree as an attribute to call in ggnetwork. 
#I could not figure out another way to set size = degree without first 
#passing it as a vertex attribute. 
abc %v% "deg_4ggnet"<-degree(abc) 

abc2<-ggnetwork(abc) 
ggplot(abc2, aes(x = x, y = y, xend = xend, yend = yend))+ 
    geom_edges(color = "black") + 
    geom_nodes(aes(color = kinds, size = deg_4ggnet)) + 
    theme_blank() 

#How to group by vertex attribute "kinds"??? 
+0

看看這篇文章:http://stackoverflow.com/q/28693826/4488105 – paqmo

回答

1

嘿,我只是用ggnet2(我沒用過ggnetwork還)開始。到目前爲止,我還沒有找到一種快速簡單的方法來讓節點按照您嘗試對它們進行分組的方式進行分組。不過,對於可以改進圖形結構的工作,我有幾點建議。

首先,安裝RColorBrewer軟件包。然後運行以下代碼:

library(igraph) 
library(ggplot2) 
library(GGally) 
library(sna) 
library(network) 
library(RColorBrewer) 

abc<-as.network(rgraph(20,1)) 
abc %v% "kinds" <- sample(letters[1:3], 10, replace = TRUE) 

ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3") 
ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3", mode = "circle") 
ggnet2(abc, color = "kinds", size="degree", size.cut=3, palette="Set3", mode = "spring") 

在第1個ggnet2函數調用中,我添加了一個調色板參數。該參數採用在RColorBrewer軟件包中預定義的調色板值。在第二次和第三次ggnet2調用中,我只是添加了模式參數表,它指定了頂點將放置在圖表可視化中的方式。我知道這並不完全回答你的問題,但我希望它有一點幫助。

相關問題