向VisNetwork Igraph添加颜色和悬停选项

时间:2016-10-07 16:36:47

标签: javascript r cluster-computing igraph htmlwidgets

我一直遇到麻烦。我只能在一个图表中获得一个或另一个但不能同时获得两个选项。下面是代码,我从@lukeA获得了很多帮助,让我到了这一步。

我有以下图表,其中我可以将群集颜色加入<select name="ID" style="border-width: 1px; width: 260px;" > <% For idx = 1 to Ubound (g_Array) %> <option value="<%response.Write(idx)%>" <% If Request.Form("ID") = idx Then Response.Write("selected") %>> <%response.write(g_Array(idx))%> </option> <% next %> </select> visNetwork

Igraph

enter image description here

然后我想在不显示标签的情况下这样做。仅当您将鼠标悬停在节点上时。因此,当我尝试应用@lukeA中的悬停选项时,颜色未显示:

library(igraph)
library(visNetwork)
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)))

col = c("#80FF00FF", "#FF0000FF", "#FF0000FF", "#00FFFFFF",
    "#FF0000FF", "#8000FFFF", "#FF0000FF", "#FF0000FF",
    "#FF0000FF", "#FF0000FF")
i96e <- set.vertex.attribute(i96e, name = "group",value = col)
i96e = graph.adjacency(g96e, mode = "undirected", weighted = TRUE, diag=FALSE)
i96e <- set.vertex.attribute(i96e, name = "group",value = col)
visIgraph(i96e, idToLabel = F, layout = "layout_nicely") %>%
  visOptions(highlightNearest = TRUE, selectedBy = "group") 

enter image description here

我不知道如何播放视频,但您可以将鼠标悬停在每个节点上,它将提供标签名称。

如何将群集颜色添加到悬停选项图?

谢谢!

1 个答案:

答案 0 :(得分:1)

以下产生了所需的结果:

# ...
i96e <- set.vertex.attribute(i96e, name = "group",value = col)
i96e = graph.adjacency(g96e, mode = "undirected", weighted = TRUE, diag=FALSE)
i96e <- set.vertex.attribute(i96e, name = "group",value = col)

V(i96e)$title <- V(i96e)$name
visIgraph(i96e, idToLabel = F, layout = "layout_nicely") %>% 
  visOptions_custom(highlightNearest = TRUE, selectedBy = "group")