gpt4 book ai didi

javascript - IGraph 中的 VisNetwork - 无法实现顶点的簇颜色

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:10 26 4
gpt4 key购买 nike

我开始使用名为 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 包,我想更改节点的颜色,以便它与聚类算法中的颜色相匹配。我的问题是我不知道如何更改节点的颜色。

vis网络示例:

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

enter image description here

我想让节点的颜色与簇的颜色相同。

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

最佳答案

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

关于javascript - IGraph 中的 VisNetwork - 无法实现顶点的簇颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com