在VisNetwork Igraph中向顶点添加颜色

时间:2016-10-05 21:59:38

标签: javascript r cluster-analysis igraph htmlwidgets

这是一个更普遍的问题:VisNetwork from IGraph - Can't Implement Cluster Colors to Vertices

我有一个igraph,我已经转换为visNetwork:

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

我有一个颜色列表,对应于我想用以下颜色绘制图形的节点:

颜色如下:

c("#80FF00FF" "#FF0000FF" "#FF0000FF" "#00FFFFFF" "#FF0000FF" "#8000FFFF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF")

如何将这些颜色添加到visNetwork的节点?

如果我这样做,它可以在正常的情节中工作,但不能在visNetwork中工作:

V(igraph)$color <- c("#80FF00FF" "#FF0000FF" "#FF0000FF" "#00FFFFFF" "#FF0000FF" "#8000FFFF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF")
plot(igraph)

由于

(如果您想拥有可重现的代码,请参阅我在帖子顶部提到的链接。)

1 个答案:

答案 0 :(得分:1)

您可以将其添加为igraph的“group”属性,默认情况下会通知节点着色。借鉴other post

中的代码
col = c("#80FF00FF", "#FF0000FF", "#FF0000FF", "#00FFFFFF",
          "#FF0000FF", "#8000FFFF", "#FF0000FF", "#FF0000FF",
          "#FF0000FF", "#FF0000FF")
i96e <- set.vertex.attribute(i96e, name = "group",value = col)
visIgraph(i96e, idToLabel = TRUE, layout = "layout_nicely")