R:当鼠标悬停在图visnetwork上时显示“弹出”信息

问题描述

我模拟了一些数据,并使用visnetwork在R中创建了一个图形网络:

library(igraph)
library(dplyr)
library(visNetwork)

#create file from which to sample from
x5 <- sample(1:100,1100,replace=T)
#convert to data frame
x5 = as.data.frame(x5)

#create first file (take a random sample from the created file)
a = sample_n(x5,1000)
#create second file (take a random sample from the created file)
b = sample_n(x5,1000)

#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")


#create graph
graph <- graph.data.frame(c,directed=F)
graph <- simplify(graph)


plot(graph)
fc <- fastgreedy.community(graph)
V(graph)$community <- fc$membership
library(visNetwork)
nodes <- data.frame(id = V(graph)$name,title = V(graph)$name,group = V(graph)$community)
nodes <- nodes[order(nodes$id,decreasing = F),]
edges <- get.data.frame(graph,what="edges")[1:2]

#visnet graph
visNetwork(nodes,edges) %>%   visIgraphLayout(layout = "layout_with_fr") %>%
    visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)

现在,该图仅在单击时显示节点信息。假设每个节点在“原始文件”中是否具有观察到的属性。例如

#add some information corresponding to the original data

other_damages_in_dollars <- rnorm(1000,104,9)

location <- c("canada","usa")

location <- sample(location,1000,replace=TRUE,prob=c(0.3,0.7))

type_of_house <- c("single","townhome","rental" )

type_of_house<- sample(type_of_house,prob=c(0.5,0.3,0.2))

#heres how the original data would have looked like

original_data = data.frame(a,b,other_damages_in_dollars,location,type_of_house)

单击每个节点时是否可以添加此信息?

 #visnet graph - is it possible to use the '$' operator to add these properties?

     visNetwork(nodes,edges) %>%   visIgraphLayout(layout = "layout_with_fr") %>%
        %>%    visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)visEvents(selectEdge = "function(properties) { alert(this.body.data.edges._data[properties.edges[0]].original_data$location); }")  %>%    visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)visEvents(selectEdge = "function(properties) { alert(this.body.data.edges._data[properties.edges[0]].original_data$type_of_house); }")  %>%    visOptions(highlightNearest = TRUE,nodesIdSelection = TRUE)visEvents(selectEdge = "function(properties) { alert(this.body.data.edges._data[properties.edges[0]].original_data$other_damage_in_dollars); }")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)