R: 鼠标悬停时显示 "popup" 信息 (graph) visnetwork

R: Display "popup" information when mouse hovers over (graph) 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 , 1000, replace=TRUE, 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); }")

您不需要活动。这内置于许多 vis.js 元素中。

所以,我将从设计工具提示的内容开始。在您制作的关于位置、房屋类型和成本的 1000 行中,我创建了一个子集,其中的行数与节点数相同。这就是我的工具提示中显示的内容。

newTitle = paste0("Location: ", toupper(location[1:nrow(nodes)]),
                  "<br>Home Type: ", type_of_house[1:nrow(nodes)],
                  "<br>Damage Related Costs: ", 
                  sprintf("$%.2f", other_damages_in_dollars[1:nrow(nodes)]))
#check it; looks okay

现在我要让我的工具提示成为我的节点的标题。

# replace the node titles:
nodes$title = newTitle

调用网络并单击图形上的任意位置一次以将其激活。现在你只需要悬停....(注意蓝色框,这意味着它正在倾听)。有很多节点非常靠近,所以当你从一个节点移动到另一个节点时会有一点延迟响应。

You can get rid of the need to click to activate with visOptions(clickToUse = F).

visNetwork(nodes, edges) %>% visIgraphLayout(layout = "layout_with_fr") 

仅供参考

我没有遍历原始问题中的所有代码;有很多!在创建我的图表之前,我将包括我 运行 的内容,因此您知道什么是在里面,什么不是。此代码未根据您的问题进行更改。

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)
fc <- fastgreedy.community(graph)
V(graph)$community <- fc$membership
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]

#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 , 1000, replace=TRUE, 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)