如何解决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 举报,一经查实,本站将立刻删除。