向"visNetwork"添加附加信息. [英] Adding additional information to a "visNetwork"

查看:96
本文介绍了向"visNetwork"添加附加信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R,我创建了一些有关一组人及其彼此之间关系的虚假数据:

Using R, I create some fake data about a group of people and their relationships to each other:

#relationship data

Data_I_Have <- data.frame(
   
    "Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
    "Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude"),
    " Place_Where_They_Met" = c("Chicago", "Boston", "Seattle", "Boston", "Paris", "Paris", "Chicago", "London", "Chicago", "London", "Paris"),
  "Years_They_Have_Known_Each_Other" = c("10", "10", "1", "5", "2", "8", "7", "10", "3", "3", "5"),
  "What_They_Have_In_Common" = c("Sports", "Movies", "Computers", "Computers", "Video Games", "Sports", "Movies", "Computers", "Sports", "Sports", "Video Games")
)

#data about individuals

additional_data_about_people <- data.frame(
   
    "Person" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xacier", "Claude", "Henry"),
   "Job" = c("Teacher", "Lawyer", "Accountant", "Engineer", "Teacher", "Lawyer", "Engineer", "Lawyer"),
"Age" = c("50", "51", "61", "56", "65", "65", "54", "50"),
"Favorite_Food" = c("pizza", "pizza", "tacos", "pizza", "ice cream", "sushi", "sushi", "pizza")
)

使用这些信息,我能够成功地建立一个表示这些人之间的关系的图形网络:

Using this information, I was able to successfully make a graph network representing the relationships between these people:

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


graph_file <- data.frame(Data_I_Have$Node_A, Data_I_Have$Node_B)


colnames(graph_file) <- c("Data_I_Have$Node_A", "Data_I_Have$Node_B")

graph <- graph.data.frame(graph_file, directed=F)
graph <- simplify(graph)

plot(graph)

nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(graph, what="edges")[1:2]

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

我认为,如果我可以在用户单击该节点时显示有关每个人的信息以及他们之间的关系的详细信息(如果可能),将会很有用.

I figured, it would be useful if I could display the information about each person when the user clicks on the node, as well as the details of their relationship (if possible).

我尝试使用"visEvents"和标题"选项( https://datastorm-open.github.io/visNetwork/nodes.html ),但我似乎无法弄清楚.有人可以告诉我该怎么做吗?

I have tried using the "visEvents" and "title" option (https://datastorm-open.github.io/visNetwork/nodes.html) in R, but I can't seem to figure it out. Could someone please show me how to do this?

谢谢

推荐答案

Data_I_Have <- data.frame(

  "Node_A" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
  "Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude"),
  "Place_Where_They_Met" = c("Chicago", "Boston", "Seattle", "Boston", "Paris", "Paris", "Chicago", "London", "Chicago", "London", "Paris"),
  "Years_They_Have_Known_Each_Other" = c("10", "10", "1", "5", "2", "8", "7", "10", "3", "3", "5"),
  "What_They_Have_In_Common" = c("Sports", "Movies", "Computers", "Computers", "Video Games", "Sports", "Movies", "Computers", "Sports", "Sports", "Video Games")
)

common_data = purrr::imap_dfc(dplyr::select(Data_I_Have, -Node_A, -Node_B), function(item, id){
  paste(id, "&colon; ", item)
})

common_strings = purrr::map_chr(seq(1, nrow(common_data)), function(in_row){
  paste(common_data[in_row, ], collapse = "<br>")
})

edge_data = dplyr::transmute(Data_I_Have, from = Node_A, to = Node_B, title = common_strings)

#data about individualsli

additional_data_about_people <- data.frame(

  "Person" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xacier", "Claude", "Henry"),
  "Job" = c("Teacher", "Lawyer", "Accountant", "Engineer", "Teacher", "Lawyer", "Engineer", "Lawyer"),
  "Age" = c("50", "51", "61", "56", "65", "65", "54", "50"),
  "Favorite_Food" = c("pizza", "pizza", "tacos", "pizza", "ice cream", "sushi", "sushi", "pizza")
)


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


graph_file <- data.frame(Data_I_Have$Node_A, Data_I_Have$Node_B)


colnames(graph_file) <- c("Data_I_Have$Node_A", "Data_I_Have$Node_B")

graph <- graph.data.frame(graph_file, directed=F)
graph <- simplify(graph)

plot(graph)

add_field = purrr::imap_dfc(additional_data_about_people, function(item, id){
  paste0(id, "&colon; ", item)
})
additional_strings = purrr::map_chr(seq(1, nrow(add_field)), function(in_row){
  paste(add_field[in_row, ], collapse = "<br>")
})
additional_df = data.frame(id = additional_data_about_people$Person, title = additional_strings)
additional_df2 = dplyr::left_join(data.frame(id = V(graph)$name), additional_df, by = "id")


nodes <- data.frame(id = V(graph)$name, title = additional_df2$title)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(graph, what="edges")[1:2]

edges2 = dplyr::left_join(edges, edge_data, by = c("from", "to"))


visNetwork(nodes, edges2)

然后在悬停时,我会看到有关每个节点和边缘的其他信息.

Then on hover, I see the additional information about each node and edge.

这里要注意两件事:

  1. visNetwork以html显示,因此您必须将html代码用于特殊字符,例如<br>用于返回,而&colon;则用于:".
  2. 所有内容都可以有一个标题",属性显示为工具提示,因此您也可以将其添加到边缘.
  1. visNetwork displays in html, so you have to use html codes for special characters, like the <br> for a return, and &colon; for the ":".
  2. Everything can have a "title" attribute that gets displayed as a tooltip, so you can add it to the edges as well.

注意,我创建了data.frame,在其中添加了属性以使其成为 look 字段,例如:",然后将它们全部粘贴在一起以显示实际标题

Notice I create the data.frame where I have the attribute added to make it look field like with the ":", and then paste them all together to make the actual title to display.

希望以上所述是有道理的.

Hopefully the above makes sense.

另外,请修复代码,在一个变量名之前留一个空格,这将对它产生奇怪的作用.

Also, fix your code, you had a space in front of one variable name that was going to do weird things to it.

就单击时显示名称而言,这超出了我的权限.

As far as displaying the names when you click on them, that's beyond me at the moment.

这篇关于向"visNetwork"添加附加信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆