按属性对网络节点进行群集

时间:2015-06-06 11:53:48

标签: r graph plot igraph edges

我有一个类似于d的data.frame,如下所示。

d <- structure(list(ID = c("KP1009", "GP3040", "KP1757", "GP2243", 
                           "KP682", "KP1789", "KP1933", "KP1662", "KP1718", "GP3339", "GP4007", 
                           "GP3398", "GP6720", "KP808", "KP1154", "KP748", "GP4263", "GP1132", 
                           "GP5881", "GP6291", "KP1004", "KP1998", "GP4123", "GP5930", "KP1070", 
                           "KP905", "KP579", "KP1100", "KP587", "GP913", "GP4864", "KP1513", 
                           "GP5979", "KP730", "KP1412", "KP615", "KP1315", "KP993", "GP1521", 
                           "KP1034", "KP651", "GP2876", "GP4715", "GP5056", "GP555", "GP408", 
                           "GP4217", "GP641"),
                    Type = c("B", "A", "B", "A", "B", "B", "B", 
                             "B", "B", "A", "A", "A", "A", "B", "B", "B", "A", "A", "A", "A", 
                             "B", "B", "A", "A", "B", "B", "B", "B", "B", "A", "A", "B", "A", 
                             "B", "B", "B", "B", "B", "A", "B", "B", "A", "A", "A", "A", "A", 
                             "A", "A"),
                    Set = c(15L, 1L, 10L, 21L, 5L, 9L, 12L, 15L, 16L, 
                            19L, 22L, 3L, 12L, 22L, 15L, 25L, 10L, 25L, 12L, 3L, 10L, 8L, 
                            8L, 20L, 20L, 19L, 25L, 15L, 6L, 21L, 9L, 5L, 24L, 9L, 20L, 5L, 
                            2L, 2L, 11L, 9L, 16L, 10L, 21L, 4L, 1L, 8L, 5L, 11L), Loc = c(3L, 
                            2L, 3L, 1L, 3L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 3L, 
                            2L, 2L, 2L, 3L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 
                            1L, 3L, 2L, 3L, 1L, 1L, 1L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L)),
               .Names = c("ID", "Type", "Set", "Loc"), class = "data.frame",
               row.names = c(NA, -48L))

我正在尝试将data.frame(d$Sets)中的集可视化为网络图。

sets <- unique(d$Set[duplicated(d$Set)])
rel <-  vector("list", length(sets))
for (i in 1:length(sets)) {
  rel[[i]] <- as.data.frame(t(combn(subset(d, d$Set ==sets[i])$ID, 2)))
}
library(data.table)
rel <- rbindlist(rel)

library(igraph)
g <- graph.data.frame(rel, directed=F, vertices=d)

V(g)$color = ifelse(V(g)$Type == "A", "red", "green")

layout <- layout.fruchterman.reingold(g, niter = 500)

plot.igraph(g, vertex.size=8,
            vertex.label.cex=0.9, layout = layout)

我根据d$Type的{​​{1}}为节点着色。

enter image description here

现在在最后的情节中,所有类型的集合都在一起。我想用一种类型的成员作为一个单独的组来绘制集合,这样最终会有三组集合。

  1. 使用类型A
  2. 的成员进行设置
  3. 使用B类
  4. 成员设置
  5. 使用A和B类型的成员进行设置
  6. 像这样enter image description here

    如何使用V(g)$Type包实现这种群集?

1 个答案:

答案 0 :(得分:3)

以下是解决问题的方法,使用上面的代码创建g对象。这比初看起来更棘手,因为您想要达到的组/连通性/集群级别的多色成员资格。:

##  Find cluster membership:
c <- clusters(g)
d <- data.frame(membership=c$membership, color=V(g)$color, id=1:length(V(g)))
c$red_members <- aggregate(d$color=="red", by=list(d$membership), FUN=sum)[,2]
c$green_members <- aggregate(d$color=="green", by=list(d$membership), FUN=sum)[,2]
V(g)$group_has_red <- (c$red_members[ c$membership ] > 0)
V(g)$group_has_green <- (c$green_members[ c$membership ] > 0)


##  Create sub-graphs containing the appropriate membership:
g_mixed <- delete.vertices(g, !(V(g)$group_has_red & V(g)$group_has_green))
g_red <- delete.vertices(g, !(V(g)$group_has_red & !(V(g)$group_has_green)))
g_green <- delete.vertices(g, !(V(g)$group_has_green & !(V(g)$group_has_red)))

par(mfrow=c(1,3))
plot(g_green, vertex.size=8, vertex.label=NA)
plot(g_mixed, vertex.size=8, vertex.label=NA)
plot(g_red, vertex.size=8, vertex.label=NA)

enter image description here