我开始使用名为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)


javascript - IGraph的VisNetwork-无法将群集颜色实现为顶点-LMLPHP

我希望节点的颜色与群集相同。

任何帮助将是巨大的,谢谢!

最佳答案

这是一个解决方案。只需使用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()

07-26 01:28