IGraph 的 VisNetwork - 无法将簇颜色实现到顶点

VisNetwork from IGraph - Can't Implement Cluster Colors to Vertices

我开始使用名为 visNetwork 的软件包,我觉得它在未来的用户界面使用方面具有很大的潜力。

尽管如此,我遇到的问题很少。我创建了一个 igraph 并且还应用了聚类算法,特别是 fastgreedy 算法。

提供的示例代码:

B = matrix( 
 c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 47, 3, 0, 3, 0, 1, 10, 13, 5,
0, 3, 19, 0, 1, 0, 1, 7, 3, 1,
0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
0, 3, 1, 0, 32, 0, 0, 3, 2, 1,
0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 2, 1, 1, 0,
0, 10, 7, 0, 3, 0, 1, 90, 12, 4, 
0, 13, 3, 0, 2, 0, 1, 12, 52, 4, 
0, 5, 1, 0, 1, 0, 0, 4, 4, 18), 
 nrow=10, 
 ncol=10)
 colnames(B) <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
 rownames(B) <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")

g96e = t(B) %*% B

i96e = graph.adjacency(g96e, mode = "undirected", weighted = TRUE, diag=FALSE)

然后我将聚类算法应用到这个:

V(i96e)$label = V(i96e)$name
V(i96e)$label.color = rgb(0,0,.2,.8)
V(i96e)$label.cex = .1
V(i96e)$size = 2
V(i96e)$color = rgb(0,0,1,.5)
V(i96e)$frame.color = V(i96e)$color
fc<-fastgreedy.community(i96e, merges=TRUE, modularity=TRUE,
                 membership=TRUE, weights=E(i96e)$weight)
colors <- rainbow(max(membership(fc)))

现在使用 visNetwork 包,我想更改节点的颜色,使其与聚类算法的颜色相匹配。我的问题是我不知道如何更改节点的颜色。

visNetwork 示例:

library(visNetwork)
visIgraph(i96e, idToLabel = TRUE, layout = "layout_nicely")%>%
visNodes(size = 10) %>%
visOptions(highlightNearest = TRUE, 
         nodesIdSelection = TRUE)     

我想让节点的颜色和簇的颜色一样。

任何帮助都会很棒,谢谢!

这是一个解决方案。只需使用 igraph 构建普通图,然后使用 visNetwork 进行外观更改:

代码:

 i96e = graph.adjacency(g96e, mode = "undirected", weighted = TRUE,    diag=FALSE)
 fc<-fastgreedy.community(i96e, merges=TRUE, modularity=TRUE,
                     membership=TRUE, weights=E(i96e)$weight)
 library (visNetwork)
 dummy <- toVisNetworkData(i96e)
 my.edges <- dummy$edges
 my.nodes <- dummy$nodes
 my.nodes$groups <- fc$membership
 my.nodes$color.background <- c("red", "blue", "green" ,"yellow")[my.nodes$groups]
 my.nodes$color.border <- c("red", "blue", "green" ,"yellow")[my.nodes$groups]
 my.nodes$size = 5
 my.edges$color <- "black"
 visNetwork(my.nodes, my.edges)%>%
 visIgraphLayout()