来自IGraph的VisNetwork - 无法将群集颜色实现到顶点 [英] VisNetwork from IGraph - Can't Implement Cluster Colors to Vertices

查看:167
本文介绍了来自IGraph的VisNetwork - 无法将群集颜色实现到顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am starting to use the package called visNetworkand I feel like it has a ton of potential for User-Interface use in the future.

虽然我遇到了一些麻烦。我创建了一个igraph,并且还应用了一个聚类算法,特别是 fastgreedy 算法。

There are few things that I am having trouble with though. I have created an igraph and have also applied a clustering algorithm to that specifically the fastgreedy algorithm.

示例代码提供:

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)

然后我将聚类算法应用于此:

Then I applied the clustering algorithm to this:

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包,我想更改节点的颜色,使其与聚类算法的颜色相匹配。我的问题是我不知道如何更改节点的颜色。

Now using the visNetwork package, I want to change the colors of the nodes so that it matches the colors from the clustering algorithm. My issue is I do not know how to change the color of the nodes.

visNetwork示例:

visNetwork Example:

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

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

I want to have the color of the nodes be the same as the clusters.

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

Any help would be great, thanks!

推荐答案

这是一个解决方案。只需使用igraph构建普通图形,然后使用visNetwork进行修饰:

Here is a solution. Just build the plain graph with igraph and then make the cosmetic changes using 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()

这篇关于来自IGraph的VisNetwork - 无法将群集颜色实现到顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆