"visNetwork"에 추가 정보 추가
Nov 03 2020
R을 사용하여 사람들과 서로의 관계에 대한 가짜 데이터를 만듭니다.
#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")
)
이 정보를 사용하여이 사람들 간의 관계를 나타내는 그래프 네트워크를 성공적으로 만들 수있었습니다.
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)
사용자가 노드를 클릭 할 때 각 사람에 대한 정보와 관계 세부 정보 (가능한 경우)를 표시 할 수 있다면 유용 할 것이라고 생각했습니다.
"visEvents"및 "title"옵션을 사용해 보았습니다 (https://datastorm-open.github.io/visNetwork/nodes.html) R에 있지만 알아낼 수없는 것 같습니다. 누군가이 방법을 보여 주시겠습니까?
감사
답변
1 rmflight Nov 04 2020 at 15:56
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, ": ", 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, ": ", 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)
그런 다음 마우스를 올리면 각 노드 및 에지에 대한 추가 정보가 표시됩니다.
여기서주의해야 할 두 가지 사항 :
- visNetwork는 html로 표시되므로
<br>반환 및:":" 과 같은 특수 문자에는 html 코드를 사용해야 합니다. - 모든 것이 툴팁으로 표시되는 "제목"속성을 가질 수 있으므로 가장자리에도 추가 할 수 있습니다.
":"필드처럼 보이 도록 속성을 추가 한 data.frame을 만든 다음 모두 붙여 넣어 실제 제목을 표시합니다.
위의 내용이 이해되기를 바랍니다.
또한 코드를 수정하면 이상한 일을 할 하나의 변수 이름 앞에 공백이 있습니다.
당신이 그들을 클릭 할 때 이름을 표시하는 것에 관해서는, 그것은 현재 저를 넘어선 것입니다.