【问题标题】:How to group nodes by vertex attribute in ggnet2 or ggnetwork如何在ggnet2或ggnetwork中按顶点属性对节点进行分组
【发布时间】:2017-04-19 05:05:01
【问题描述】:

我想使用 GGally 或 ggnetwork 绘制一个网络对象,并且我希望能够生成一个布局,其中节点按顶点属性分组。我花了一些时间寻找一种方法来做到这一点,但还没有弄清楚。是否可以在布局中按属性对节点进行分组,使得属性为“a”的所有节点都在一个集群中,属性“b”的节点都在一个集群中,等等?

提前致谢。

这里有两个例子:

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"???

【问题讨论】:

标签: r plot ggplot2 statnet


【解决方案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")

在第一个 ggnet2 函数调用中,我添加了一个调色板参数。此参数采用 RColorBrewer 包中预定义的调色板值。在第 2 次和第 3 次 ggnet2 调用中,我刚刚添加了模式参数,它指定了顶点在图形可视化中的放置方式。我知道这并不能完全回答您的问题,但希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 2021-03-23
    • 2017-03-29
    • 1970-01-01
    • 2021-09-14
    • 2015-05-17
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多