R : 색상 및 ID로 선택

Nov 23 2020

R에 다음 데이터가 있습니다.

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")
)

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")
)

이 정보를 사용하여 "ID로 선택"할 수있는 대화 형 그래프를 만들었습니다.

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)


nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]

colors = data.frame( "id" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry"), 
                     "color" = c("red", "blue", "green", "black", "red", "blue", "black", "blue") )

nodes <- merge(nodes, colors, by = "id")
edges <- get.data.frame(graph, what="edges")[1:2]

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

코드는 "id"대신 "color"로 선택할 수 있도록 수정할 수 있습니다.

 nodes$color = colors$color nodes <- data.frame(id = V(graph)$name, title = V(graph)$name) nodes <- nodes[order(nodes$id, decreasing = F),]
 
 colors = data.frame( "id" = c("John", "Peter", "Tim", "Kevin", "Adam", "Xavier", "Claude", "Henry"), 
                      "color" = c("red", "blue", "green", "black", "red", "blue", "black", "blue") )
 
 nodes <- merge(nodes, colors, by = "id")
 edges <- get.data.frame(graph, what="edges")[1:2]
 
 visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
     visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(selectedBy = "color") 

이 두 가지를 함께 결합하는 방법이 있습니까? 두 개의 검색 창을 가질 수 있습니까? 하나는 ID 용이고 다른 하나는 색상 용입니다.

    visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
     visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>% visOptions(selectedBy = "color") 

visNetwork(nodes, edges,  main = "A really simple example", width = "100%") %>%   visIgraphLayout(layout = "layout_with_fr") %>%
    visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
    visLegend()

이 같은: https://imgur.com/a/Y9yY0Q7

감사

답변

PLY Nov 24 2020 at 17:31

코드의 마지막 부분을

visNetwork(nodes, edges, main = "test", width = "100%") %>% 
  visIgraphLayout(layout = "layout_with_fr") %>% 
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE,
             selectedBy = list(multiple = TRUE, variable = "color"))

이 결과

는, 범례를 추가 할 아마도 당신이 당신의 dataframe을 다시 구성해야하며, @Daman 깊은 주석하는 링크를 확인합니다.